无法使用Python在Selenium中找到引导下拉列表元素

2024-09-29 02:23:53 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图找到下拉列表并选择一个选项。我使用的代码如下

from selenium import webdriver

driver=webdriver.Chrome(r"C:\Users\nithi\Downloads\chromedriver.exe")

driver.get('https://parivahan.gov.in/parivahan/')

VehicleServicesButton = driver.find_element_by_xpath('//*[@id="block-block-9"]/div/div[1]/p/a')

VehicleServicesButton.click()

OtherStatesButton = driver.find_element_by_xpath('//*[@id="node-1978"]/div/div/div/div/div[3]/a/div/img') 

OtherStatesButton.click()

driver.maximize_window()
driver.implicitly_wait(20)

driver.find_element_by_id('state_cd_label').click()

我在最后一行代码中遇到以下错误

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"[id="state_cd_label"]"}
  (Session info: chrome=88.0.4324.104)

这里是html代码和网站! enter image description here


Tags: 代码dividbydriverseleniumelementfind
1条回答
网友
1楼 · 发布于 2024-09-29 02:23:53

你很快就能做到。单击图像时,新选项卡打开,因此要在新窗口上操作,您必须将驾驶员焦点从父选项卡切换到新选项卡上

一旦你这样做,例外情况就会消失

要切换到新选项卡,只需在上述代码的最后一行之前添加此代码

p = driver.current_window_handle
print("Parent window"+p)
chwd = driver.window_handles
print( chwd)
driver.switch_to.window(chwd[1])

输出-

enter image description here

相关问题 更多 >