ElementNotInteractableException in Selenium occurs when you attempt to interact with an element on a web page, but the element is present and visible, yet it cannot be interacted with due to some reason. Here are different scenarios when an ElementNotInteractableException can occur, along with coding examples:
Element Covered by Another Element:
Element with Zero Dimensions
Disabled Form Elements
Invisible Elements
Read-only Elements:
Hidden Elements:
Non-Input Elements with Invalid Actions:
Element Covered by Another Element:
This occurs when the element you want to interact with is covered or obscured by another element.
from selenium import webdriver
from selenium.common.exceptions import ElementNotInteractableException
driver = webdriver.Chrome()
try:
driver.get("https://example.com")
element_to_click = driver.find_element_by_id("element-to-click")
covering_element = driver.find_element_by_id("covering-element")
covering_element.click() # Raises ElementNotInteractableException
except ElementNotInteractableException as e:
print(f"ElementNotInteractableException: {e}")
finally:
driver.quit()
Element with Zero Dimensions:
If an element has zero width or height, it may not be interactable.
from selenium import webdriver
from selenium.common.exceptions import ElementNotInteractableException
driver = webdriver.Chrome()
try:
driver.get("https://example.com")
zero_dimension_element = driver.find_element_by_id("zero-dimension-element")
zero_dimension_element.click() # Raises ElementNotInteractableException
except ElementNotInteractableException as e:
print(f"ElementNotInteractableException: {e}")
finally:
driver.quit()
Disabled Form Elements:
Disabled input fields, buttons, or form elements cannot be interacted with.
from selenium import webdriver
from selenium.common.exceptions import ElementNotInteractableException
driver = webdriver.Chrome()
try:
driver.get("https://example.com")
disabled_button = driver.find_element_by_id("disabled-button")
disabled_button.click() # Raises ElementNotInteractableException
except ElementNotInteractableException as e:
print(f"ElementNotInteractableException: {e}")
finally:
driver.quit()
Invisible Elements:
Elements that are set to display: none or are otherwise not visible cannot be interacted with.
from selenium import webdriver
from selenium.common.exceptions import ElementNotInteractableException
driver = webdriver.Chrome()
try:
driver.get("https://example.com")
invisible_element = driver.find_element_by_id("invisible-element")
invisible_element.click() # Raises ElementNotInteractableException
except ElementNotInteractableException as e:
print(f"ElementNotInteractableException: {e}")
finally:
driver.quit()
Read-only Elements:
Read-only input fields cannot be modified.
from selenium import webdriver
from selenium.common.exceptions import ElementNotInteractableException
driver = webdriver.Chrome()
try:
driver.get("https://example.com")
read_only_input = driver.find_element_by_id("read-only-input")
read_only_input.send_keys("Text") # Raises ElementNotInteractableException
except ElementNotInteractableException as e:
print(f"ElementNotInteractableException: {e}")
finally:
driver.quit()
Hidden Elements:
Elements that are hidden using CSS properties like visibility: hidden or opacity: 0 cannot be interacted with.
from selenium import webdriver
from selenium.common.exceptions import ElementNotInteractableException
driver = webdriver.Chrome()
try:
driver.get("https://example.com")
hidden_element = driver.find_element_by_id("hidden-element")
hidden_element.click() # Raises ElementNotInteractableException
except ElementNotInteractableException as e:
print(f"ElementNotInteractableException: {e}")
finally:
driver.quit()
Non-Input Elements with Invalid Actions:
Trying to perform actions like sending keys or clicking on non-input elements (e.g.,
, ) that do not support those actions.from selenium import webdriver
from selenium.common.exceptions import ElementNotInteractableException
driver = webdriver.Chrome()
try:
driver.get("https://example.com")
non_input_element = driver.find_element_by_id("non-input-element")
non_input_element.send_keys("Text") # Raises ElementNotInteractableException
except ElementNotInteractableException as e:
print(f"ElementNotInteractableException: {e}")
finally:
driver.quit()
Handling ElementNotInteractableException usually involves ensuring that the element is in an interactable state before attempting to interact with it. You can use explicit waits, check for element attributes, or adjust your test logic to accommodate the element's state.
Latest comments (0)