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

7

93

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:

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.

Paten answered 7/2, 2018 at 13:25 Comment(3)
What browser are you using?Hepatitis
Firefox and TorPaten
What code are you using to perform the click? Do you continue to see this same exception if you attempt the click after the white box disappears?Hepatitis
W
153

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.

Wilbertwilborn answered 7/2, 2018 at 15:49 Comment(5)
Pradeep, would you mind explaining on why the execute script does? It worked for me, but I have no idea whyAccordingly
@Accordingly its a click done via javascript, instead of via the webdriver element click. why the webdriver fails to 'click' sometimes, i dont know, but i think it has to do with some validation checks it does first, whereas javascript doesnt care, it just clicks.Depoliti
If you are going to use a CSS selector to specify a class, do it the proper way, div.loadingWhiteBox. The way you have it is a string comparison and may or may not work. For example, if there is a class on another element called "loadingWhiteBox-abc", your CSS selector will find it since it's doing a partial text match when that's not the class you want.Rigadoon
2nd didn't work for me, but the first was amazing, you are amazingHypothalamus
And in newer versions of selenium, one must do driver.find_element(By.CSS_SELECTOR, ...)Hellen
A
43

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
Apriorism answered 14/3, 2020 at 21:4 Comment(2)
thanks for this you should put the imports and define each element at the top. otherwise top answer as always.Doom
@Datanovice Done, adding the imports.Apriorism
B
12

You can wait until the element gone,

WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.invisibilityOfElementLocated(By.className("loadingWhiteBox")));
Brei answered 25/3, 2019 at 12:27 Comment(1)
This is not a valid Python code.Synthiasyntonic
V
2

for Selenide

WebElement element = selenide_element.toWebElement();
WebDriverRunner.driver().executeJavaScript("arguments[0].click();", element);
Veranda answered 25/2, 2021 at 13:32 Comment(1)
This is not a valid Python code.Synthiasyntonic
P
1

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.

Pula answered 31/1, 2020 at 14:58 Comment(4)
Where are you getting this driver.getInnerDriver()? Is this custom to your framework?Sori
Sorry, I corrected the code. I have a custom implementation of firefox driver and just copied the code without noticing it.Pula
This is how you do it in python: driver.execute_script("arguments[0].click();", element). Where element is the web element you are trying to click.Pula
This is not a valid Python code.Synthiasyntonic
A
1

I faced the same problem and I just used this :

elm = driver.find_elements_by_css_selector('div[class*="loadingWhiteBox"]')
elm.click()
Appressed answered 25/10, 2020 at 1:24 Comment(0)
S
0

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));
    }
}
Stralsund answered 18/9, 2022 at 8:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.