简体   繁体   English

如何点击列表中的<li>中的元素<ul> python中含有硒的元素?

[英]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 :-如果您能够打开下拉项目但无法单击项目,您应该尝试使用WebDriverWaitExplicit 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.

相关问题 如何点击<a>元素</a>列表<ul><a>和</a><li><a>元素与硒使用python?</a> - How to click on the list of the <a> elements in an <ul> and <li> elements with selenium using python? 如何获得列表<li>中的元素<ul>使用 Python 使用 Selenium?</ul></li> - How to get a list of the <li> elements in an <ul> with Selenium using Python? 如何在 python 中使用 Selenium 在 ul 中单击 li? - How do I click an li inside a ul using Selenium in python? Python:如何使用BeautifulSoup抓取前3个 <li> ul = class的元素? - Python: How to use BeautifulSoup to scrape the first 3 <li> elements of a ul=class? 点击列表元素 selenium python - click through list elements with selenium python Selenium: Select 并直接迭代<li> a的子元素<ul>父母使用 python?</ul></li> - Selenium: Select and iterate through direct <li> child elements of a <ul> parent using python? Python Selenium:get_elements 方法没有获取 ul 中的 li 项 - Python Selenium: get_elements method does not get li items in ul 如何提取全部 <li> 下的元素 <ul> - How to extract all <li> elements under <ul> Selenium 如何单击存储在列表中的元素 - Selenium how to click over elements stored in a list Python + Selenium:如何单击“ onclick”元素? - Python + Selenium: How can click on “onclick” elements?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM