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

我正在努力刮一个页面,但有时点击链接/按钮会有困难。

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

有什么办法可以解决这个问题吗? 我已经试过使用以下命令:

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

但是元素即使在不活跃的时候也是存在的。

144935 次浏览

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.

You can wait until the element gone,

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

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.

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.

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 WebDriverWait inconjunction with the WebDriverWaitWebDriverWait for 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 WebDriverWait inconjunction with the expected_conditions for 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.XPATH, "//div[@class='loadingWhiteBox']")))
      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[@class='taLnk ulBlueLinks']"))).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.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']"))))
      

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

I faced the same problem and I just used this : elm = driver.find_elements_by_css_selector('div[class*="loadingWhiteBox"]') elm.click()

for Selenide

WebElement element = selenide_element.toWebElement();
WebDriverRunner.driver().executeJavaScript("arguments[0].click();", element);

Here is a general method to delete up to 10 intercepting elements:

  public void clickAndDeleteInterceptingElement(By selector) {
int numberOfTries = 3;
boolean isExceptionExisting = true;
while (numberOfTries > 0 && isExceptionExisting) {
numberOfTries--;
try {
clickElement(findElement(selector));
isExceptionExisting = false;
} catch (ElementClickInterceptedException e) {
String element = e.getMessage().split("Other element would receive the click:")[1];
element = element.split(">")[0];
element = element.replace(" <", "");
String tag = element.split(" ")[0];
String attributes = "[" + element.replace(tag + " ", "") + "]";
String resultString = "";
boolean isInsideAttributeValue = false;
boolean areInvertedCommasOpeningOnes = true;
for (int i = 0; i < attributes.length(); i++) {
char c = attributes.charAt(i);
if (c == '"' && areInvertedCommasOpeningOnes) {
isInsideAttributeValue = true;
areInvertedCommasOpeningOnes = false;
} else if (c == '"' && !areInvertedCommasOpeningOnes) {
isInsideAttributeValue = false;
areInvertedCommasOpeningOnes = true;
}
if (c == ' ' && isInsideAttributeValue) {
resultString += "spaceInsideAttributeValue";
} else {
resultString += c;
}
}
resultString = resultString.replace(" ", "][");
resultString = resultString.replace("spaceInsideAttributeValue", " ");
String cssSelectorString = tag + resultString;
try {
deleteElement(By.cssSelector(cssSelectorString));
} catch (WebDriverException e2) {
e.printStackTrace();
e2.printStackTrace();
break;
}
sleep(1000);
}
}
if (isExceptionExisting) {
clickElement(findElement(selector));
}

}