繁体   English   中英

Selenium Python:没有找到明显存在的元素

[英]Selenium Python: Not finding element which clearly exists

我正在尝试使用 python 和 selenium 单击站点导航的级别。导航栏包含其中包含子元素的列表项。

这是导航栏的 html:

在此处输入图像描述

这里的目标是找到 id="ts_time" 到 hover 的元素,然后单击其中的元素。

到目前为止,我已经尝试了以下选择类型:

  • ID
  • XPath
  • 班级名称

这是身份证件。

time_menu_button = driver.find_element(By.ID, "ts_time")
ActionChains(driver).move_to_element(time_menu_button)

time.sleep(2.5)

这会导致NoSuchElementException

要在id="ts_time"的元素上执行 hover,您需要调用perform()然后单击其中的元素,您可以使用以下任一定位器策略

time_menu_button = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "li#ts_time")))
ActionChains(driver).move_to_element(time_menu_button).peform()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "li#ts_time > a"))).click()
  • 注意:您必须添加以下导入:

     from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC

暂无
暂无

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

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