【问题标题】:How to click on the anchor element when the spinner obscures it using Selenium and Python?当微调器使用 Selenium 和 Python 遮挡锚元素时,如何单击锚元素?
【发布时间】:2020-04-26 17:16:51
【问题描述】:

我是 selenium 的新手,我一直在尝试点击一个锚元素。我已经尝试过 css-selector,lint_text,xpath,absolute xpath 但我仍然无法单击它,而是收到此错误消息:

selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: Driver Schedule

有谁知道如何解决这个问题?

更新:我现在收到此错误:

selenium.common.exceptions.ElementClickInterceptedException: Message: Element <a href="#/schedule/weekly-view"> is not clickable at point (326,333) because another element <div class="app-spinner-layer active"> obscures it

【问题讨论】:

  • 不要将代码发布为图片。
  • 检查你的元素是否在任何
  • 其实是个管理面板,代码里面有密码,不能分享,剩下的代码可以分享。
  • 没有

标签: python-3.x selenium xpath css-selectors webdriverwait


【解决方案1】:

click() 元素上的文本为 Driver Schedule 因为它是一个 &lt;a&gt; 节点,您必须诱导 WebDriverWait element_to_be_clickable(),您可以使用以下任一Locator Strategies

  • 使用LINK_TEXT

    WebDriverWait(driver, 20).until(EC.invisibility_of_element((By.CSS_SELECTOR, "div.app-spinner-layer.active")))
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.LINK_TEXT, "Driver Schedule"))).click()
    
  • 使用PARTIAL_LINK_TEXT

    WebDriverWait(driver, 20).until(EC.invisibility_of_element_located((By.XPATH, "//div[@class='app-spinner-layer active']")))
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.PARTIAL_LINK_TEXT, "Driver Schedule"))).click()
    
  • 使用CSS_SELECTOR

    WebDriverWait(driver, 20).until(EC.invisibility_of_element((By.CSS_SELECTOR, "div.app-spinner-layer.active")))
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "li.pll a[href$='weekly-view']"))).click()
    
  • 使用XPATH

    WebDriverWait(driver, 20).until(EC.invisibility_of_element_located((By.XPATH, "//div[@class='app-spinner-layer active']")))
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[contains(@href,'weekly-view') and contains(., 'Driver Schedule')]"))).click()
    
  • 注意:您必须添加以下导入:

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

【讨论】:

猜你喜欢
相关资源
最近更新 更多
热门标签