无法单击Splinter / Selenium中的Element:ElementClickInterceptedException

yel*_*ays 11 python selenium exception splinter

我正在尝试抓取页面,但有时无法单击链接/按钮。

加载网页时,只要该框出现在网站上,“ loadingWhiteBox”将首先出现,然后在几秒钟后消失(但它将保留在HTML代码中),我无法单击链接,并且得到以下错误信息:

selenium.common.exceptions.ElementClickInterceptedException: Message: 
Element <span class="taLnk ulBlueLinks"> is not clickable at point 
(318.3000030517578,661.7999877929688) because another element <div 
class="loadingWhiteBox"> obscures it
Run Code Online (Sandbox Code Playgroud)

有什么办法可以解决此问题?我已经尝试使用以下命令:

driver.is_element_present_by_css('div[class*="loadingWhiteBox"]')
Run Code Online (Sandbox Code Playgroud)

但是即使该元素不处于活动状态,它也存在。

Deb*_*anB 20

这个错误信息...

selenium.common.exceptions.ElementClickInterceptedException: Message: Element <span class="taLnk ulBlueLinks"> is not clickable at point (318.3000030517578,661.7999877929688) because another element <div class="loadingWhiteBox"> obscures it
Run Code Online (Sandbox Code Playgroud)

...暗示所需的元素不可点击,因为其他一些元素遮住了它。


有多种方法可以解决此问题,其中几种方法如下:

  • 当您打算调用时,click()您需要将WebDriverWaitWebDriverWait WebDriverWait结合起来element_to_be_clickable(),您可以使用以下任一定位器策略

  • 如果错误......另一个元素掩盖了它......首先仍然存在,您需要将WebDriverWait与阻塞元素的expected_conditionsfor 相invisibility_of_element()结合,如下所示:

    • 使用CSS_SELECTOR

      WebDriverWait(driver, 20).until(EC.invisibility_of_element((By.CSS_SELECTOR, "div.loadingWhiteBox")))
      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "span.taLnk.ulBlueLinks"))).click()
      
      Run Code Online (Sandbox Code Playgroud)
    • 使用XPATH

      WebDriverWait(driver, 20).until(EC.invisibility_of_element((By.XPATH, "//div[@class='loadingWhiteBox']")))
      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[@class='taLnk ulBlueLinks']"))).click()
      
      Run Code Online (Sandbox Code Playgroud)
  • 如果问题仍然存在,您可以使用以下execute_script()方法:

    • 使用CSS_SELECTOR

      WebDriverWait(driver, 20).until(EC.invisibility_of_element((By.CSS_SELECTOR, "div.loadingWhiteBox")))
      driver.execute_script("arguments[0].click();", WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "span.taLnk.ulBlueLinks"))))
      
      Run Code Online (Sandbox Code Playgroud)
    • 使用XPATH

      WebDriverWait(driver, 20).until(EC.invisibility_of_element((By.XPATH, "//div[@class='loadingWhiteBox']")))
      driver.execute_script("arguments[0].click();", WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[@class='taLnk ulBlueLinks']"))))
      
      Run Code Online (Sandbox Code Playgroud)

笔记

您必须添加以下导入:

from selenium.webdriver.support.ui import WebDriverWait       
from selenium.webdriver.common.by import By       
from selenium.webdriver.support import expected_conditions as EC
Run Code Online (Sandbox Code Playgroud)


小智 10

你可以等到元素消失,

WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.invisibilityOfElementLocated(By.className("loadingWhiteBox")));
Run Code Online (Sandbox Code Playgroud)


Pra*_*bar 9

您可以尝试以下2种方法来单击element。

element = driver.find_element_by_css('div[class*="loadingWhiteBox"]')
driver.execute_script("arguments[0].click();", element)

element = driver.find_element_by_css('div[class*="loadingWhiteBox"]')
webdriver.ActionChains(driver).move_to_element(element ).click(element ).perform()
Run Code Online (Sandbox Code Playgroud)

希望这会起作用。

  • 对你有帮助吗? (3认同)
  • Pradeep,您介意解释一下为什么执行脚本会这样做吗?它对我有用,但我不知道为什么 (3认同)
  • 第二个对我不起作用,但第一个太棒了,你太棒了 (3认同)
  • @toceto通过JavaScript而不是通过webdriver元素单击来完成单击。我不知道为什么Webdriver有时有时无法“单击”,但我认为它必须先进行一些验证检查,而javascript不在乎,它只是单击即可。 (2认同)