[英]How to click on the list of the <li> elements in an <ul> elements with selenium in python?
I tried to select 2002 in dropdown menu.我试图在下拉菜单中选择 2002。 It doesn't work at any late.
它在任何时候都不起作用。 I used xpath
我用过xpath
driver.find_element_by_xpath("html/body/main/div/form/div[3]/div[1]/section/div[3]/fieldset/div[7]/dl[1]/dd/ul/li[1]/a").click()
but it doesn't work..I tried all the solutions I got... How can I select this?但它不起作用......我尝试了我得到的所有解决方案......我该如何选择?
If you're able to open dropdown item but unable to click on item, you should try usingExplicit Waits
with WebDriverWait
to wait until this element is visible and enable to click as below :-如果您能够打开下拉项目但无法单击项目,您应该尝试使用
WebDriverWait
Explicit Waits
等待该元素可见并启用单击如下: -
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
element = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "ul#ulBirthYear a[data-value='2002']")))
element.click()
Or或者
element = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.LINK_TEXT, "2002")))
element.click()
First of all, try to avoid using absolute XPATH.首先,尽量避免使用绝对 XPATH。 Use something like this:
使用这样的东西:
'//ul[@id="uiBirthYear"]/li/a[@data-value="2002"]'
Also ensure, that the DOM is fully built, before you trying to get/click on this element.在尝试获取/单击此元素之前,还要确保 DOM 已完全构建。
Try to set an implicit wait尝试设置隐式等待
driver.implicitly_wait(10)
or an explicit wait (read more: http://selenium-python.readthedocs.io/waits.html )或显式等待(阅读更多: http : //selenium-python.readthedocs.io/waits.html )
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.