87

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.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
yellow days
  • 1,053
  • 2
  • 9
  • 11

7 Answers7

142

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.

Pradeep hebbar
  • 2,147
  • 1
  • 9
  • 14
  • 4
    Pradeep, would you mind explaining on why the execute script does? It worked for me, but I have no idea why – toceto Aug 30 '19 at 13:44
  • 9
    @toceto 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. – n00b Sep 12 '19 at 15:07
  • 1
    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. – JeffC Dec 07 '19 at 23:28
  • 3
    2nd didn't work for me, but the first was amazing, you are amazing – tariksalay Jul 02 '20 at 19:31
40

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
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
11

You can wait until the element gone,

WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.invisibilityOfElementLocated(By.className("loadingWhiteBox")));
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
hakki atas
  • 119
  • 1
  • 2
2

for Selenide

WebElement element = selenide_element.toWebElement();
WebDriverRunner.driver().executeJavaScript("arguments[0].click();", element);
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.

Codrut
  • 435
  • 4
  • 11
1

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

moe_
  • 219
  • 2
  • 3
  • 15
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));
}

}