[英]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.