ElementNotInteractableException

Introduction

While automating web applications, Selenium frequently interacts with web elements by clicking buttons, entering text into input fields, selecting dropdown values, and performing various user actions. However, there are situations where Selenium successfully locates an element, but the element cannot be interacted with.

When Selenium attempts to interact with such an element, it raises an ElementNotInteractableException.

This exception usually occurs when an element exists in the Document Object Model (DOM) but is hidden, disabled, or otherwise unavailable for user interaction. It is commonly encountered while automating modern web applications that dynamically show or hide elements based on user actions.

In this tutorial, you will learn what ElementNotInteractableException is, why it occurs, how to handle it properly, practical examples, common mistakes, best practices, and frequently asked interview questions.


What is ElementNotInteractableException?

ElementNotInteractableException is raised when Selenium successfully locates an element, but the element cannot receive user interactions such as clicking or typing.

For example:

Locate Element
       │
       ▼
Element Exists?
      /      \
    No        Yes
    │          │
    ▼          ▼
NoSuchElement  Can Selenium
 Exception     Interact?
                  │
               Yes/No
               /    \
             Yes     No
              │       │
              ▼       ▼
         Continue   ElementNotInteractableException
         Execution         Raised

Although the element exists on the webpage, Selenium cannot interact with it because it is not currently available for user interaction.


Why Does ElementNotInteractableException Occur?

Some common reasons include:

  • Hidden elements.

  • Disabled elements.

  • Elements that are outside the visible viewport.

  • Dynamic webpage behavior.

  • Timing and synchronization issues.

  • Elements covered by CSS properties such as display:none.

  • Elements that become interactable only after specific user actions.


Practical Example

The following example dynamically creates a hidden input field using JavaScript. Although Selenium successfully locates the element, it cannot enter text because the element is hidden from the user.

As a result, Selenium raises ElementNotInteractableException.

import pytest
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.common.exceptions import (
    ElementNotInteractableException,
)


# Topic: ElementNotInteractableException
# Practice site: https://www.testmuai.com/selenium-playground/
# Run: pytest -s 61_examples/test_05_element_not_interactable_exception.py
#
# ElementNotInteractableException is raised when an element exists in the DOM
# but cannot be interacted with, such as when it is hidden.


def test_element_not_interactable_exception():
    driver = webdriver.Chrome()

    try:
        driver.get(
            "https://www.testmuai.com/selenium-playground/"
        )

        driver.execute_script(
            """
            const hidden = document.createElement('input');
            hidden.id = 'hidden-field';
            hidden.type = 'text';
            hidden.style.display = 'none';
            document.body.appendChild(hidden);
            """
        )

        with pytest.raises(
            ElementNotInteractableException
        ):
            driver.find_element(
                By.ID,
                "hidden-field"
            ).send_keys("cannot type")

    finally:
        driver.quit()

Output

Chrome browser launched successfully.

Website opened successfully.

Hidden input field created successfully.

Selenium located the element successfully.

Element is not interactable.

ElementNotInteractableException raised successfully.

Exception handled successfully.

Test Executed Successfully.

Note: The exception is expected in this example. PyTest treats the test as successful because pytest.raises() explicitly verifies that ElementNotInteractableException is raised.


Understanding the Code

Import Required Modules

import pytest

from selenium import webdriver
from selenium.webdriver.common.by import By

from selenium.common.exceptions import (
    ElementNotInteractableException,
)

Imports:

  • Selenium WebDriver.

  • Locator strategies.

  • ElementNotInteractableException.

  • PyTest for exception validation.


Launch Chrome Browser

driver = webdriver.Chrome()

Creates a new Chrome browser session.


Open the Website

driver.get(
    "https://www.testmuai.com/selenium-playground/"
)

Opens the Selenium Playground website.


Create a Hidden Element

driver.execute_script(
    """
    JavaScript Code
    """
)

The JavaScript code:

  • Creates a text input field.

  • Assigns it the ID hidden-field.

  • Sets its display property to none.

  • Adds it to the webpage.

Although the element exists in the DOM, users cannot see or interact with it.


Attempt to Enter Text

driver.find_element(
    By.ID,
    "hidden-field"
).send_keys("cannot type")

Selenium successfully locates the element but cannot send text to it because it is hidden.


Verify the Exception

with pytest.raises(
    ElementNotInteractableException
):
    driver.find_element(
        By.ID,
        "hidden-field"
    ).send_keys("cannot type")

pytest.raises() verifies that Selenium raises the expected exception.

If the exception occurs successfully, the test passes.


Close the Browser

driver.quit()

Closes all browser windows and properly ends the WebDriver session.


Execution Flow

Launch Browser
       │
       ▼
Open Website
       │
       ▼
Create Hidden Element
       │
       ▼
Locate Element
       │
       ▼
Attempt Interaction
       │
       ▼
Is Element Interactable?
      /      \
    Yes       No
    │          │
    ▼          ▼
 Continue    Raise
 Execution   ElementNotInteractableException
                  │
                  ▼
          Verify Exception Using PyTest
                  │
                  ▼
              Close Browser

Automation Testing Example

Suppose an application hides its search field until a user clicks a Search button.

Click Search Button
        │
        ▼
Search Field Appears
        │
        ▼
Enter Search Text

If Selenium attempts to enter text before the field becomes visible:

ElementNotInteractableException

Proper synchronization is required before interacting with such elements.


Real-World Example

Modern applications frequently display elements conditionally.

Examples include:

  • Hidden menus.

  • Search fields.

  • Login forms.

  • Expandable sections.

  • Dynamically loaded components.

Element Exists
       │
       ▼
Hidden Using CSS
       │
       ▼
Selenium Finds Element
       │
       ▼
Attempt Interaction
       │
       ▼
ElementNotInteractableException

Understanding how webpages dynamically display elements is extremely important for building reliable automation frameworks.


Common Mistakes Beginners Make

Ignoring Element Visibility

Incorrect

username.send_keys("admin")

without verifying whether the element is visible.


Better

username.is_displayed()

Always verify that an element is visible before interacting with it.


Not Waiting for Dynamic Elements

Modern applications frequently display elements after:

  • AJAX requests.

  • Button clicks.

  • Animations.

  • JavaScript execution.

Explicit waits can help synchronize Selenium with these changes.


Confusing It with NoSuchElementException

Many beginners assume the element does not exist.

However:

NoSuchElementException

        ≠

ElementNotInteractableException

The differences are:

NoSuchElementException

Element does not exist.

----------------------------

ElementNotInteractableException

Element exists but cannot be interacted with.

Understanding this distinction is extremely important during debugging.


Best Practices

  • Verify that elements are visible before interacting with them.

  • Use explicit waits whenever appropriate.

  • Synchronize Selenium with dynamic webpage behavior.

  • Avoid interacting with hidden or disabled elements.

  • Prefer normal Selenium interactions over JavaScript workarounds whenever possible.

  • Understand the application’s UI flow before writing automation scripts.


Conclusion

ElementNotInteractableException occurs whenever Selenium locates an element successfully but cannot interact with it. Hidden elements, disabled controls, and dynamically displayed components are among the most common causes of this exception.

Proper synchronization techniques, visibility checks, and understanding the application’s behavior significantly improve automation reliability and reduce flaky test failures.

Mastering Selenium exceptions is an important step toward building scalable and maintainable automation frameworks.


Frequently Asked Questions (FAQs)

What is ElementNotInteractableException?

It is raised when Selenium locates an element successfully but cannot interact with it.


What causes this exception?

Common causes include:

  • Hidden elements.

  • Disabled elements.

  • Dynamic webpage behavior.

  • Timing issues.

  • Elements that are not currently visible.


How can I avoid this exception?

You can avoid it by:

  • Using explicit waits.

  • Verifying element visibility.

  • Synchronizing Selenium with dynamic webpages.

  • Understanding when elements become interactable.


What is the difference between NoSuchElementException and ElementNotInteractableException?

  • NoSuchElementException occurs when Selenium cannot locate an element.

  • ElementNotInteractableException occurs when Selenium locates the element but cannot interact with it.


Why do we use pytest.raises() in this example?

pytest.raises() verifies that Selenium raises the expected exception, allowing us to validate Selenium’s behavior during testing.


Key Takeaways

  • ElementNotInteractableException occurs when Selenium locates an element that cannot receive user interactions.

  • Hidden and dynamically displayed elements are common causes of this exception.

  • Element visibility should always be verified before performing interactions.

  • Explicit waits significantly improve synchronization with modern web applications.

  • Understanding the difference between Selenium exceptions simplifies debugging.

  • pytest.raises() can be used to validate expected exceptions during testing.

  • Proper synchronization techniques improve automation framework reliability.

  • ElementNotInteractableException is an important Selenium automation and interview topic.