Understanding element not Interactable Exception in Selenium
Last Updated :
20 Aug, 2024
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();
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_11zonWhy 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.
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.
Similar Reads
Understanding No Such Element Exception in Selenium
Selenium is a popular open-source tool for automating web browser interactions and it is commonly used in testing web applications. Selenium provides a robust framework for simulating user actions like clicking buttons, filling the forms and navigating the pages. One of the common issues encountered
8 min read
Understanding Stale Element Reference Exception in Selenium
Selenium is a widely used tool for interacting with web elements programmatically in web automation testing. However, one of the common challenges testers face is the Stale Element Reference Exception. This exception occurs when a previously located web element is no longer available in the DOM (Doc
6 min read
Understanding execute async script in Selenium Using java
Understanding executeAsyncScript in Selenium Using Java is crucial for handling asynchronous JavaScript operations during test automation. The executeAsyncScript method allows Selenium to wait for certain operations, like AJAX calls or timeouts, to complete before proceeding. This is particularly us
2 min read
tag_name element method - Selenium Python
Seleniumâs Python Module is built to perform automated testing with Python. Selenium Python bindings provides a simple API to write functional/acceptance tests using Selenium WebDriver. To open a webpage using Selenium Python, checkout - Navigating links using get method â Selenium Python. Just bein
2 min read
How to wait until an element is present in Selenium?
Selenium is a powerful tool for automating web browsers, often used for testing web applications. However, web pages that load content dynamically can present challenges, especially when you need to interact with elements that may not be immediately available. In such cases, waiting for an element t
2 min read
Locating single elements in Selenium Python
Locators Strategies in Selenium Python are methods that are used to locate elements from the page and perform an operation on the same. Seleniumâs Python Module is built to perform automated testing with Python. Selenium Python bindings provide a simple API to write functional/acceptance tests using
5 min read
How to Run Internet Explorer Driver in Selenium Using Java?
Selenium is a well-known software used for software testing purposes. Selenium consists of three parts. One is Selenium IDE, one is Selenium Webdriver & the last one is Selenium Grid. Among these Selenium Webdriver is the most important one. Using Webdriver online website testing can be done. Th
6 min read
Understand Java Interface using Selenium WebDriver
Java Interface is a blueprint of the class that contains static constants and abstract methods. It cannot have a method body. The interface is a mechanism to achieve abstraction. This article focuses on discussing the Selenium WebDriver Interface. Example: public interface Animal { void printAnimalN
4 min read
How to Scroll an Element into View in Selenium?
A high-level programming language that helps users in building web applications is called Java. It is not only used for creating web applications but it can also be used for automating web applications through various automation tools. Selenium is one such tool, which gives users the capability to a
4 min read
Python - find_element() method in Selenium
While performing any action on a web page using selenium, there is need of locators to perform specific tasks. Locators in web page are used to identify unique elements within a webpage. Web elements could be anything that the user sees on the page, such as title, table, links, buttons, toggle butto
2 min read