python - Selenium Python : Element not clickable because another element obscures it

标签 python selenium-webdriver xpath css-selectors webdriverwait

我想单击一个尚未可单击或上面有其他元素的按钮。

但是一旦它可以点击,我就想按下它。 我试过这个:

button = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.CLASS_NAME, "likeClick")))
button.click()

但是它不起作用,因为我仍然收到错误

selenium.common.exceptions.ElementClickInterceptedException: Message: Element <a class="likeClick"> is not clickable at point (400,553) because another element <div class="delete-overlay"> obscures it```

最佳答案

此错误消息...

selenium.common.exceptions.ElementClickInterceptedException: Message: Element <a class="likeClick"> is not clickable at point (400,553) because another element <div class="delete-overlay"> obscures it

...意味着点击<a>元素失败,因为元素被 <div> 遮挡。元素似乎是一个临时覆盖


解决方案

要单击所需的元素,您需要输入 WebDriverWait对于 invisibility_of_element_located() overlay的然后诱导WebDriverWait对于 element_to_be_clickable()您可以使用以下任一 locator strategies :

  • 使用CSS_SELECTOR:

    WebDriverWait(driver, 30).until(EC.invisibility_of_element_located((By.CSS_SELECTOR, "div.delete-overlay")))
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a.likeClick"))).click()
    
  • 使用XPATH:

    WebDriverWait(driver, 30).until(EC.invisibility_of_element_located((By.XPATH, "//div[@class='delete-overlay']")))
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[@class='likeClick']"))).click()
    
  • 注意:您必须添加以下导入:

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

关于python - Selenium Python : Element not clickable because another element obscures it,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71458460/

相关文章:

python - cassandra-driver 执行查询时,cassandra-driver 返回错误 OperationTimedOut

python - python带键功能的排序列表,获得意外的订单

python - 如何通过 CQL 直接连接 Cassandra 的示例 (1)

xslt - 如何使用 xsl 从给定的 xml 中获取前 x 次出现

javascript - 子节点匹配时获取父元素节点

python - 如何从txt文件中获取列表(Python)

selenium-webdriver - Selenium Javascript Webdriver : how to setup Firefox profile to accept selfsigned certificate

selenium-webdriver - 如何使用 RemoteWebDriver 设置浏览器语言

java - 使用另一个 WebElement 时获取 WebElement

c# - BizTalk 服务器问题