Python 无法单击元素:Splinter / Selenium 中的 ElementClickInterceptedException

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/48665001/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 18:48:07  来源:igfitidea点击:

Can not click on a Element: ElementClickInterceptedException in Splinter / Selenium

pythonseleniumwebdriverwaitsplinterexpected-condition

提问by yellow days

I'm trying to scrape a page, but I sometimes have trouble clicking a link/button.

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

When the web page loads, then the "loadingWhiteBox" will appear first and then disappear after a few seconds (but it will remain in the HTML code) as long as the box is appears on the website, I can not click on the link and get following error message:

当网页加载时,“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

Is there any way to work around this? I've already tried working with the following command:

有没有办法解决这个问题?我已经尝试过使用以下命令:

driver.is_element_present_by_css('div[class*="loadingWhiteBox"]')

But the element is present even when it's not active.

但是即使元素不活动,它也存在。

回答by Pradeep hebbar

You can try the below 2 methods to click on 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()

hope this will work.

希望这会奏效。

回答by hakki atas

You can wait until the element gone,

你可以等到元素消失,

WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.invisibilityOfElementLocated(By.className("loadingWhiteBox")));

回答by DebanjanB

This error message...

这个错误信息...

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

...implies that the desired element wasn't clickable as some other element obscures it.

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



There are multiple approaches to address this issue and a couple of them are as follows:

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

  • As you intent to invoke click()you need to induce WebDriverWaitinconjunction with the expected_conditionsfor the element_to_be_clickable()and you can use either of the following Locator Strategies:

    • Using CSS_SELECTOR:

      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "span.taLnk.ulBlueLinks"))).click()
      
    • Using XPATH:

      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[@class='taLnk ulBlueLinks']"))).click()
      
  • Incase the error ...another element obscures it...still persists first you need to induce WebDriverWaitinconjunction with the expected_conditionsfor the invisibility_of_element()of the blocking element as follows:

    • Using 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()
      
    • Using XPATH:

      WebDriverWait(driver, 20).until(EC.invisibility_of_element((By.CSS_SELECTOR, "span.taLnk.ulBlueLinks")))
      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@class='loadingWhiteBox']"))).click()
      
  • If the issue still persists you can use the execute_script()method as follows:

    • Using 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"))))
      
    • Using XPATH:

      WebDriverWait(driver, 20).until(EC.invisibility_of_element((By.CSS_SELECTOR, "span.taLnk.ulBlueLinks")))
      driver.execute_script("arguments[0].click();", WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@class='loadingWhiteBox']"))))
      
  • 当你打算调用click()需要引起WebDriverWaitinconjunction与expected_conditionselement_to_be_clickable(),你可以使用以下的定位策略

    • 使用CSS_SELECTOR

      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "span.taLnk.ulBlueLinks"))).click()
      
    • 使用XPATH

      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[@class='taLnk ulBlueLinks']"))).click()
      
  • 如果错误......另一个元素掩盖了它......首先仍然存在,您需要将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()
      
    • 使用XPATH

      WebDriverWait(driver, 20).until(EC.invisibility_of_element((By.CSS_SELECTOR, "span.taLnk.ulBlueLinks")))
      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@class='loadingWhiteBox']"))).click()
      
  • 如果问题仍然存在,您可以使用以下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"))))
      
    • 使用XPATH

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


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

回答by Codrut

When I get this error I usually try a different approach. Instead of:

当我收到此错误时,我通常会尝试不同的方法。代替:

driver.findElement(By.cssSelector("div[class*="loadingWhiteBox"]")).click();

Try this:

尝试这个:

WebElement webElement = driver.findElement(By.cssSelector("div[class*="loadingWhiteBox"]"));
((JavascriptExecutor) driver).executeScript("arguments[0].click();", webElement);

This will click the found webElement even when there are overlays.

即使有叠加层,这也会单击找到的 webElement。

If this is not working then be sure that you are trying to click the correct 'clickable' web element and check that your css selector is not pointing to a different webElement. By 'clickable' I mean a webElement that performs an action when you click it (for example opening a new page). The web driver will click it and you may think it didn't actually performed the click action, but it actually performed it on the wrong webElement.

如果这不起作用,请确保您尝试单击正确的“可点击”Web 元素并检查您的 css 选择器是否指向不同的 webElement。“可点击”是指当您单击它时执行操作的 webElement(例如打开一个新页面)。Web 驱动程序将单击它,您可能认为它实际上并未执行单击操作,但它实际上是在错误的 webElement 上执行的。

回答by Gautam Bothra

The error appears because another element with same class or with same xpath/css appears on the screen.

出现错误是因为屏幕上出现了另一个具有相同类或相同 xpath/css 的元素。

Try giving some wait methods until the element appears such Thread.sleep(), wait().

尝试给出一些等待方法,直到元素出现,例如 Thread.sleep()、wait()。