Open In App

Understanding element not Interactable Exception in Selenium

Last Updated : 20 Aug, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Selenium, a popular automation tool for web applications, often encounters the ElementNotInteractableException. This common Selenium exception occurs when an element is present in the DOM but cannot be interacted with. Understanding the causes and solutions for ElementNotInteractableException is crucial for effective Selenium test automation.

This article explores why this exception happens and provides strategies to handle it efficiently.

What is an ElementNotInteractableException?

ElementNotInteractableException is an exception thrown by Selenium WebDriver when an element is present in the DOM but cannot be interacted with. This exception typically occurs in the following scenarios:

  • Not Visible or Hidden: The element is present in the DOM but is not visible on the screen, often due to CSS properties like display: none; or visibility: hidden;.
  • Disabled: The element is disabled, meaning it cannot be interacted with, such as a disabled input field or button.
  • Outside the Viewport: The element is located outside the visible area of the browser's viewport, making it inaccessible for interactions like clicks or typing.
  • Overlapped by Another Element: Another element is covering or overlapping the intended element, blocking it from being interacted with.
  • Not Completely Rendered: The element has not fully loaded or rendered on the page, which can occur in pages with dynamic content or slow-loading elements.

When Does an ElementNotInteractableException Occur?

An ElementNotInteractableException occurs in the following scenarios:

  • Element is not visible: The element exists in the DOM but is not visible to the user.
  • Element is disabled: The element is present but is in a disabled state.
  • Element is behind another element: Another element is overlapping the target element.
  • Element is out of view: The element is not within the current viewport.
  • Incorrect frame: The element is within an iframe that is not currently focused.

How to Handle ElementNotInteractableException in Selenium

Handling this exception effectively requires various strategies, depending on the root cause. Here are some common methods:

1. Wait for the element to be visible

Use an explicit wait to wait for the element to be clickable before interacting with it. This ensures the element is fully loaded and visible.

Java
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("elementId")));
element.click();


2. Scroll into the view

Scroll the element into the viewport to make it interactable. This is useful for elements not initially visible in the viewport.

Java
WebElement element = driver.findElement(By.id("elementId"));
((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", element);
element.click();


3. Enable the web element

Ensure the element is enabled before interacting with it. Check if the element is enabled before trying to interact with it. If it's disabled, handle the situation accordingly.

Java
WebElement element = driver.findElement(By.id("elementId"));
if (!element.isEnabled()) {
    // Code to enable the element
}
element.click();

4. Handle overlapping elements

Check for and handle any overlapping elements that might be blocking interaction. If another element overlaps the target element, dismiss or move the blocking element.

Java
WebElement element = driver.findElement(By.id("elementId"));
WebElement overlappingElement = driver.findElement(By.id("overlappingElementId"));
// Code to handle the overlapping element
overlappingElement.click();
element.click();


5. Handle switching to the correct frame

Ensure you have switched to the correct frame before interacting with the element. If the element is inside a frame, switch to that frame before interacting with the element.

Java
driver.switchTo().frame("frameName");
WebElement element = driver.findElement(By.id("elementId"));
element.click();

Example of Handling ElementNotInteractableException in Selenium

Java
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.Assert;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

import java.time.Duration;

public class ElementNotInteractableExample {

    WebDriver driver;

    @BeforeTest
    public void setUp() {
        // Initialize ChromeDriver
        driver = new ChromeDriver();
    }

    @Test
    public void verifyPageTitle() {
        // Open the webpage
        driver.get("https://example.com/");
        
        // Initialize WebDriverWait with a timeout of 10 seconds
        WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
        
        // Find the element which may not be initially interactable
        WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("a[href='https://www.iana.org/domains/example']")));
        
        // Perform action on the element
        element.click();
        
        // Verify the page URL
        Assert.assertEquals(driver.getCurrentUrl(), "https://www.iana.org/domains/example");
    }

    @AfterTest
    public void tearDown() {
        // Close the browser
        if (driver != null) {
            driver.quit();
        }
    }
}


Output:

Element-Not-Interactable-Exception-in-Selenium-output
Element Not Interactable Exception in Selenium output_11zon

Why is Testing on Real Devices and Browsers Important?

Testing on real devices and browsers is crucial because it ensures your application works as expected across different environments. Emulators and simulators may not replicate real-world conditions accurately, leading to undetected issues that can affect user experience.

Key reasons for testing on real devices and browsers

  • Accuracy: Real devices provide accurate results and behaviors.
  • User Experience: Ensures a seamless user experience across different devices and browsers.
  • Bug Detection: Helps detect device-specific and browser-specific bugs that may not appear on emulators.

Why Use Tools to Automate Selenium Tests?

Automating Selenium tests offers several benefits:

  • Efficiency: Automates repetitive tasks, saving time and effort.
  • Consistency: Ensures consistent test execution and results.
  • Coverage: Increases test coverage by running tests across multiple environments quickly.
  • Feedback: Provides rapid feedback to developers, facilitating faster bug fixes.
  • Scalability: Enables running large test suites in parallel, improving testing efficiency.

Conclusion

Handling the ElementNotInteractableException in Selenium is essential for robust and reliable web automation. By implementing techniques such as waiting for element visibility, scrolling into view, enabling elements, addressing overlapping elements, and switching to the correct frame, you can effectively manage this exception.

Testing on real devices and browsers further ensures accuracy and a seamless user experience. Leveraging automation tools for Selenium tests enhances efficiency, consistency, and scalability, leading to high-quality web applications. Understanding and applying these strategies will empower you to overcome ElementNotInteractableException challenges in Selenium.


Next Article
Article Tags :

Similar Reads