简体   繁体   English

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

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

I'm trying to fill out a form on this site: https://p2c.coweta.ga.us/p2c/jailinmates.aspx 我正在尝试在此网站上填写表格: https//p2c.coweta.ga.us/p2c/jailinmates.aspx
(click "filter inmate list", then click + button to add a row) (单击“过滤囚犯列表”,然后单击+按钮添加一行)

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

When I run the code as shown the second drop down menu on the second row seems to be hidden. 当我如图所示运行代码时,第二行的第二个下拉菜单似乎被隐藏了。 the stack trace returns: 堆栈跟踪返回:

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

I've tried different things to work around this, such as explicit waits, or wait until element visible, but none of that worked. 我尝试了其他方法来解决此问题,例如显式等待,或等到元素可见为止,但是这些都不起作用。

I also have tried driver.execute_script: 我也尝试过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)

This didn't give an error, but it didn't actually select the option( perhaps because its hidden?). 这没有给出错误,但是实际上没有选择该选项(也许是因为它隐藏了?)。

Is there a way I can select the option im trying to get that is being hidden? 有没有办法我可以选择隐藏的选项?

It might be worth noting the same drop down menu has a default class that seems like it might be laying on top or something. 可能值得一提的是,同一下拉菜单中的默认类似乎位于顶部或其他位置。

I managed to get it working by changing the last line to: 我设法通过将最后一行更改为:

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

I believe the selector that you have provided is not unique and selenium is getting confused. 我相信您提供的选择器不是唯一的,并且硒越来越混乱。

You can also get the selectors directly by right clicking the element in the inspection menu and selecting copy selector . 您还可以通过右键单击检查菜单中的元素并选择“ copy selector器”来直接获得选择copy selector

在此处输入图片说明

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

相关问题 Python Selenium选择下拉菜单选项 - Python Selenium Select Drop Down Menu Option 如何使用 select 这个非 select 下拉菜单中的元素使用 Selenium Webdriver 和 ZA7F57F35426B9387411 - How to select this element from this non select drop-down menu using Selenium Webdriver and Python 如何通过Selenium和python从下拉菜单中选择元素? - How to select element from drop-down menu via selenium and python? 如何从Python / Selenium的此下拉菜单中选择一个元素? - How can I select an element from this drop down menu in Python/Selenium? 如何使用 Selenium 和 Python 从下拉菜单中的 select 项目? - How to select item from drop down menu using Selenium with Python? 如何从 python selenium 的下拉菜单中获取 select 的值 - How to select a value from drop down menu in python selenium 单击不是 SELECT 元素的下拉菜单的第一个元素-Python selenium - Clicking first element of drop down menu that is not a SELECT element -Python selenium Selenium 有没有办法从谷歌表单(python)的下拉菜单中选择一个选项 - Is there a way in Selenium to select an option from a drop down menu in google forms (python) 如何使用Selenium - Python选择下拉菜单选项值 - How to select a drop-down menu option value using Selenium - Python 使用Selenium和python从下拉菜单中选择一个选项 - seleting an option from a drop down menu using selenium and python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM