繁体   English   中英

Python:如何从Selenium的下拉菜单中选择选项,元素已隐藏

[英]Python: How to select option from drop down menu in Selenium, element hidden

我正在尝试在此网站上填写表格: https//p2c.coweta.ga.us/p2c/jailinmates.aspx
(单击“过滤囚犯列表”,然后单击+按钮添加一行)

def coweta_search(last, first):
    print("Coweta County Jail")
    url = "https://p2c.coweta.ga.us/p2c/jailinmates.aspx"
    driver = webdriver.Chrome(chrome_options=(), executable_path="/usr/bin/chromedriver")
    wait = WebDriverWait(driver, 30)
    driver.get(url)
    driver.find_element_by_css_selector("#btnGridSearch > span:nth-child(2)").click()
    driver.find_element_by_css_selector(".field0 > option:nth-child(7)").click()
    driver.find_element_by_css_selector("input.default").send_keys(last)
    driver.find_element_by_css_selector(".ui-icon-plusthick").click()
    driver.find_element_by_css_selector("tr.sf:nth-child(2) > td:nth-child(1) > select:nth-child(1) > option:nth-child(2)").click()

    driver.find_element_by_css_selector("tr.sf:nth-child(2) > td:nth-child(2) > select:nth-child(2) > option:nth-child(7)").click()


    return driver

当我如图所示运行代码时,第二行的第二个下拉菜单似乎被隐藏了。 堆栈跟踪返回:

element not interactable: Element is not currently visible and may not be manipulated 

我尝试了其他方法来解决此问题,例如显式等待,或等到元素可见为止,但是这些都不起作用。

我也尝试过driver.execute_script:

first_drop = driver.find_element_by_css_selector("tr.sf:nth-child(2) > td:nth-child(2) > select:nth-child(2) > option:nth-child(7)")
driver.execute_script("arguments[0].click();", first_drop)

这没有给出错误,但是实际上没有选择该选项(也许是因为它隐藏了?)。

有没有办法我可以选择隐藏的选项?

可能值得一提的是,同一下拉菜单中的默认类似乎位于顶部或其他位置。

我设法通过将最后一行更改为:

driver.find_element_by_css_selector("tr.sf:nth-child(2) > td:nth-child(2) > select.field1 > option:nth-child(7)").click()

我相信您提供的选择器不是唯一的,并且硒越来越混乱。

您还可以通过右键单击检查菜单中的元素并选择“ copy selector器”来直接获得选择copy selector

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM