You might have been using Selenium in web automation testing as it is quite a popular framework. However, like any other tool, Selenium can sometimes cause issues in the form of unexpected errors.

In this blog, we will learn more about a common Selenium error: org.openqa.selenium.elementnotinteractableexception. Simply put, it means the element is not interactable.

We will try to understand the root causes and practical solutions of this exception. Whether you are an experienced automation engineer or just starting your Selenium journey, you will find this blog useful.

Troubleshoot effectively and write more powerful and useful test scripts!

What is ‘org.openqa.selenium.elementnotinteractableexception’?

The ElementNotInteractableException is telling you that even though Selenium found the element, it can’t interact with it right now. This error can occur when Selenium tries to interact with a web element (like clicking a button, entering text in a text field, etc.), but it isn’t possible to do so at that moment.

11 Reasons for Selenium’s ElementNotInteractableException

There are a few reasons why an element is present in the DOM, but it is unavailable to Selenium for interaction.

  1. Element is Not Visible

    Sometimes, an element might be present in the HTML code but isn’t visible on the screen. This could be due to CSS rules that hide the element, such as setting display: none or visibility: hidden.

    Example: Suppose you are trying to click a button that is hidden in a popup. If the button is hidden when Selenium tries to click it, you might see this error. Even though Selenium can find it, it can’t click on it. You would need to wait for the element to become visible before interacting with it.

  2. Element is Disabled

    Another common reason for this exception is when the element is disabled. Disabled elements (like buttons or input fields) cannot be interacted with. In HTML, a button or input field with the disabled attribute cannot be clicked or typed into.

    Example: Consider a form where a “Submit” button is disabled until all required fields are filled in. You can’t click on it until it’s enabled.

  3. Element is Covered by Another Element

    Sometimes, an element may be covered by another element, such as a loading spinner, popup, or advertisement. If this happens, Selenium will be able to find the element but can’t click on it because something else is on top of it.

    Example: A “Sign In” button is covered by a loading spinner or popup. The “Sign In” button is visible, but something (like a spinner or popup) is blocking it. This makes it impossible for Selenium to interact with the button.

  4. Element is Not in the Ready State

    Some elements, such as dropdowns or editable fields, may need to be expanded or in focus before interacting with them. If you try to interact with such elements before they are ready, you might get this exception.

    Example: A dropdown that needs to be clicked to open before selecting an option. The dropdown might be collapsed, or not in the right state for interaction when Selenium tries to click it.

  5. Element is Outside the Viewport (Not on the Visible Part of the Page)

    Sometimes, an element might be located outside the visible area of the web page, and Selenium can’t interact with it because it’s not currently visible on the screen. For example, if the element is at the bottom of a long page and you haven’t scrolled down to it yet, Selenium might not be able to click it.

  6. Timing Issues (Page Not Fully Loaded)

    If you try to interact with an element before the page is fully loaded, the element might not be ready yet. This can happen if your script runs too quickly and tries to interact with the element before it’s rendered on the page.

    Example: Trying to click a “Sign In” button right after navigating to the login page, but the page is still loading.

  7. Element in an iFrame or a New Window

    If an element is inside an iframe (an embedded frame on the page), Selenium might not be able to interact with it unless you switch to the frame first. Similarly, if the element is in a new window, you need to switch to that window to interact with the element.

    Example: Trying to interact with a button inside an iframe without switching to that iframe first.

  8. Element is Read-Only or Unmodifiable

    Sometimes, an element might be read-only, meaning you can’t type into it or modify its value. For example, an input field could be set to readonly, or a text box may be non-editable.

  9. Element is Not in the Correct State for Interaction

    Some elements, like checkboxes, may need to be in a specific state before you can interact with them. For example, a checkbox might need to be checked before clicking or unchecking it, or a dropdown might need to be opened first.

  10. Dynamic Content or JavaScript Issues

    Some websites use JavaScript to dynamically update content or load elements after the page has initially loaded. If you try to interact with an element that hasn’t fully loaded or rendered yet due to JavaScript execution, you may get this exception.

    Example: Trying to click on a button that only appears after some JavaScript finishes loading data.

  11. JavaScript or CSS Errors

    Sometimes, there might be errors in the page’s JavaScript or CSS that prevent elements from being rendered or functioning correctly. These issues can cause Selenium to find the element but fail to interact with it.

    Example: A button or input field might not be clickable because of JavaScript errors on the page.

How to Handle Element Not Interactable in Selenium

Now, we have seen why you might be facing this error. Let us see the common reasons and how to fix them with the help of examples.

Wait for the Element to Become Visible or Enabled

Often, the issue happens because the element is either not visible or not yet enabled on the page. To resolve this, your script can wait until the element is visible and ready for interaction.

Fix it by using WebDriverWait to wait until the element is visible or enabled.

Here’s a code snippet to help explain this.
WebDriver driver = new ChromeDriver();
	
driver.get("https://example.com");

// Wait for the element to be visible
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement button = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("submitButton")));
button.click(); // Now it will click when the button is visible

Here, visibilityOfElementLocated ensures that the button is both present and visible on the screen.

Scroll to the Element

Sometimes, an element might be outside the visible area of the page (e.g., at the bottom or top). So, you need to scroll to it and then interact with it.

Fix it by scrolling to the element.

Here’s a code snippet to help explain this.
WebDriver driver = new ChromeDriver();

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

// Find the button and scroll to it
WebElement button = driver.findElement(By.id("nextButton"));
((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", button);
button.click(); // Now it will click after scrolling the button into view

Here, use of scrollIntoView makes sure that the element is brought into the visible area before performing the click.

Check the Element is Not Covered by Another Element

If an element is covered by another (like a loading spinner, popup, or advertisement), Selenium cannot interact with it. To fix this, you need to make sure that the covering element disappears before Selenium tries to interact with the target element.

Fix it by waiting for the element on top to disappear.

Here’s a code snippet to help explain this.
WebDriver driver = new ChromeDriver();

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

// Wait for the spinner to disappear before interacting with the button
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.invisibilityOfElementLocated(By.id("spinner")));

// Now, find and click the Sign In button
WebElement signInButton = driver.findElement(By.id("signInButton"));
signInButton.click(); // Now it can click after the spinner is gone

Here, invisibilityOfElementLocated waits until the spinner is no longer visible, allowing you to safely interact with the button.

Switch to the Correct Frame or Window

An element can be inside an iframe (a separate frame within the page) or a new window. So, you must switch to that frame or window first, and then the script should interact with the element.

Fix it by switching to the iframe before interacting with the element.

Here’s a code snippet to help explain this.
WebDriver driver = new ChromeDriver();

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

// Switch to the iframe containing the element
driver.switchTo().frame("iframeName");

// Now find and click the button inside the iframe
WebElement button = driver.findElement(By.id("buttonInsideIframe"));
button.click();

Here, switchTo().frame("iframeName") switches the context to the iframe, allowing you to interact with the button inside it.

Check the Element is Editable (for Text Inputs)

You may be trying to type into a text field that is readonly or disabled. Selenium cannot interact with it. Make sure the element is enabled or editable.

You can fix it by checking if the element is editable and enabled.

Here is a code snippet to help explain this.
WebDriver driver = new ChromeDriver();

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

// Find the input field
WebElement inputField = driver.findElement(By.id("inputField"));

// Check if the input field is enabled before typing
if (inputField.isEnabled() && !inputField.getAttribute("readonly").equals("true")) {
  inputField.sendKeys("Hello, World!");
} else {
  System.out.println("The input field is either disabled or readonly.");
}

Here, we check if the field is enabled and not readonly before typing into it.

Make Sure the Element is Enabled

Check if you are trying to click a button or interact with an element that is disabled (for example, a “Submit” button that is only enabled after filling out a form). If so, make sure the element is enabled first.

You can fix it by waiting for the element to be clickable.

Here’s a code snippet to help explain this.
WebDriver driver = new ChromeDriver();

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

// Wait until the button is clickable (enabled and visible)
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement submitButton = wait.until(ExpectedConditions.elementToBeClickable(By.id("submitButton")));
submitButton.click(); // Now the button can be clicked when it's enabled

elementToBeClickable waits for the element to be both visible and enabled before clicking it.

Wait Till Element is Fully Loaded

Sometimes, the page may still be loading, or the element hasn’t yet been rendered on the page. In this case, use explicit waits to make sure the element is fully loaded before trying to interact with it.

Here’s a code snippet to help explain this.
WebDriver driver = new ChromeDriver();

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

// Wait for the button to be visible before clicking
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement button = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("submitButton")));
button.click();

This ensures that the button is fully visible before interacting with it.

Conclusion

While you may find these Selenium exceptions as frustrating, they still help us learn more and enhance our skills. By understanding what the exception is trying to tell us and taking appropriate measures, you can create reliable test scripts that run without giving flakey results.

More Like This: