Features

Handling ElementClickInterceptedException with Selenium

  • Share on Facebook
  • Share on Twitter
  • Share on LinkedIn
  • Share on HackerNews

In this article we'll study the ElementClickInterceptedException, which is an exception that may occur during your Selenium tests.

ElementClickInterceptedException is part of a collection of exceptions that Selenium might throw during your test. Other exceptions such as NoSuchElementException, TimeoutException, SessionNotCreatedException, StaleElementReferenceException and ElementClickInterceptedException each deserve their own article.

What is an ElementClickInterceptedException?

An ElementClickInterceptedException is a type of exception which can occur when using Selenium to run a test. The exception is usually thrown when an attempt to click on an element on a web page is intercepted or blocked by another element. Usually this happens when another element is overlapping or positioned (partly) in front of the DOM element that your Selenium script is trying to click.

ElementClickInterceptedException is a Java-specific exception when running Selenium tests through the Java bindings. The same type of exception is available in the other Selenium bindings, with different names:

Selenium Binding Exception name
Java ElementClickInterceptedException
JavaScript WebDriverError: element click intercepted
C#/.NET OpenQA.Selenium.ElementClickInterceptedException
Ruby ERROR: element click intercepted
Python selenium.common.exceptions.ElementClickInterceptedException

What can cause an ElementClickInterceptedException?

Below is a list of the most common scenarios that may lead to this type of exception.

  • Overlapping Elements

    For example, when multiple DOM elements overlap on a web page and the DOM element you want to click is obscured by another element. The most common elements that may cause such behaviour are popups, modal dialogs, advertisements or overlapping buttons.

  • Animations and Transitions

    DOM Elements might move during a CSS animation or transition, while your test is trying to interact with an element. This may cause problems for Selenium to click the element.

  • Timing Problems

    ElementClickInterceptedException can occur if the Selenium test script attempts to click an element before it becomes clickable or visible on the page.

How can I handle an ElementClickInterceptedException in Selenium?

Handling an ElementClickInterceptedException in Selenium involves adding strategies to wait for DOM elements to become clickable. You'll also need to handle situations where elements are intercepted or obscured by other elements in the DOM tree.

Use Explicit Waits

You can use explicit waits, with expected conditions, to make sure that the element is clickable, before Selenium attempts to click it. See the Java example below.

Copy code
import org.openqa.selenium.*;

public class ElementClickInterceptException {
    public static void main(String[] args) {
        WebDriver driver = new ChromeDriver();
        WebDriverWait wait = new WebDriverWait(driver, 10);

        // Replace this with the DOM element you are trying to click

        By elementLocator = By.id("clickMyElement");

        driver.get("https://testingbot.com");

        try {
            WebElement element = wait.until(ExpectedConditions.elementToBeClickable(elementLocator));
            element.click(); // once the element is clickable, you can click it

        } catch (Exception e) {
            // The element was not clickable, handle it appropriately

            e.printStackTrace();
        } finally {
            driver.quit();
        }
    }
}

Add a Retry Mechanism to your test

You can add a retry mechanism to handle the ElementClickInterceptedException.. Simply catch the ElementClickInterceptedException during your test. Then wait for a specific amount of time, or hide/remove any overlapping elements. Finally, retry the click action.

Copy code
import org.openqa.selenium.*;
import org.openqa.selenium.support.ui.WebDriverWait;

public class RetryMechanismExample {
    public static void main(String[] args) {
        WebDriver driver = new ChromeDriver();
        WebDriverWait wait = new WebDriverWait(driver, 10);

        // Replace this with the DOM element you are trying to click

        By elementLocator = By.id("clickMyElement");
        
        driver.get("https://testingbot.com");

        int maxAttempts = 3; // Maximum number of times you want to retry

        int attempt = 1;
        boolean elementClickable = false;

        while (attempt <= maxAttempts) {
            try {
                WebElement element = wait.until(ExpectedConditions.elementToBeClickable(elementLocator));
                element.click(); // Click the element once it becomes clickable

                elementClickable = true; // Set the flag to indicate it was clicked

                break; // Break the loop since the action was successful

            } catch (Exception e) {
                // Not clickable, maybe retry?

                attempt++; // Increment the attempt count

            }
        }

        if (!elementClickable) {
            System.out.println("Element was not clickable after " + maxAttempts + " attempts. Try a different strategy?");
        }
        driver.quit();
    }
}

Scroll the Element into View

If the element is not visible on the page, because it's not in the viewport, then you can scroll it into view before attempting to click on it.

Copy code
driver.execute_script("arguments[0].scrollIntoView();", element)
element.click()

Handling any Overlapping Elements

If you know there are overlapping elements on the page, you can first hide/remove these from the DOM before you attempt to click.

Use the Selenium Actions Class

You could use the Selenium Action Class, which allows you to combine multiple user gestures. For example, combine moving to the element and then clicking the element in one action.

Copy code
import org.openqa.selenium.*;

public class ActionsClickExample {
    public static void main(String[] args) {
        WebDriver driver = new ChromeDriver();
        Actions actions = new Actions(driver);

        // Replace this with the DOM element you are trying to click

        By elementLocator = By.id("clickMyElement");

        driver.get("https://testingbot.com");

        WebElement element = driver.findElement(elementLocator);
        actions.moveToElement(element).click().build().perform();

        driver.quit();
    }
}
  • Share on Facebook
  • Share on Twitter
  • Share on LinkedIn
  • Share on HackerNews
TestingBot Logo

Sign up for a Free Trial

Start testing your apps with TestingBot.

No credit card required.

Other Articles

Record tests with Playwright

Learn how to use a Playwright Recorder to easily record tests.

Read more
Testing with React and Selenium

Learn how to run Selenium tests against a React based website.

Read more
How to fix Flaky Tests

Learn about Flaky Tests: why they happen and how to fix these, with various examples.

Read more
Test Automation with ChatGPT

An introduction into generating test scripts with ChatGPT's AI language model.

Read more
Selenium and Generative AI

Generate realistic looking test data to be used with your Selenium Automated Tests.

Read more
Migrate from Appium 1.x to Appium 2.x

Learn how to migrate from Appium 1 to the new Appium 2.

Read more