当微调器使用 Selenium 和 Python 遮挡锚元素时,如何单击锚元素? [英] How to click on the anchor element when the spinner obscures it using Selenium and Python?

查看:24
本文介绍了当微调器使用 Selenium 和 Python 遮挡锚元素时,如何单击锚元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 selenium 的新手,我已经尝试了一段时间来单击锚元素.我已经尝试过 css-selector,lint_text,xpath,absolute xpath 但我仍然无法单击它,而是收到以下错误消息:

I am new to selenium and i have been trying for some time to click on an anchor element. I have tried css-selector,lint_text,xpath,absolute xpath but i am still unable to click on it and instead i am getting this error message:

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

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

Does anyone know how to get around this ?

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

Update: I am getting this error now:

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

推荐答案

在元素上click(),文本为Driver Schedule,因为它是一个<a> 节点您必须为 element_to_be_clickable() 诱导 WebDriverWait 并且您可以使用以下任一 定位器策略:

To click() on the element with text as Driver Schedule as it is an <a> node you have to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following 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()
    

  • 注意:您必须添加以下导入:

  • Note : You have to add the following imports :

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

  • 这篇关于当微调器使用 Selenium 和 Python 遮挡锚元素时,如何单击锚元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆