Debug School

rakesh kumar
rakesh kumar

Posted on

Explain ElementNotVisibleException exception

ElementNotVisibleException in Selenium occurs when an element is present in the DOM (Document Object Model) but is not visible on the web page. This typically happens when an element is hidden through CSS properties like display: none or visibility: hidden. Here are different scenarios when an ElementNotVisibleException can occur, along with coding examples:
Hidden by CSS

Hidden by JavaScript

Hidden in Overflow or Off-Screen

Overlayed Elements

Element Outside the Viewport

Element Hidden by User Actions

Hidden by CSS:

This scenario occurs when an element is hidden using CSS properties like display: none or visibility: hidden.

from selenium import webdriver
from selenium.common.exceptions import ElementNotVisibleException

driver = webdriver.Chrome()
try:
    driver.get("https://example.com")
    hidden_element = driver.find_element_by_id("hidden-element")
    hidden_element.click()  # Raises ElementNotVisibleException
except ElementNotVisibleException as e:
    print(f"ElementNotVisibleException: {e}")
finally:
    driver.quit()
Enter fullscreen mode Exit fullscreen mode

Hidden by JavaScript:

JavaScript can be used to hide or manipulate the visibility of elements on a page.

from selenium import webdriver
from selenium.common.exceptions import ElementNotVisibleException

driver = webdriver.Chrome()
try:
    driver.get("https://example.com")
    js_hidden_element = driver.find_element_by_id("js-hidden-element")
    js_hidden_element.click()  # Raises ElementNotVisibleException
except ElementNotVisibleException as e:
    print(f"ElementNotVisibleException: {e}")
finally:
    driver.quit()
Enter fullscreen mode Exit fullscreen mode

Hidden in Overflow or Off-Screen:

Elements that are outside the viewport or hidden within scrollable containers are not visible and can raise this exception.

from selenium import webdriver
from selenium.common.exceptions import ElementNotVisibleException

driver = webdriver.Chrome()
try:
    driver.get("https://example.com")
    offscreen_element = driver.find_element_by_id("offscreen-element")
    offscreen_element.click()  # Raises ElementNotVisibleException
except ElementNotVisibleException as e:
    print(f"ElementNotVisibleException: {e}")
finally:
    driver.quit()
Enter fullscreen mode Exit fullscreen mode

Overlayed Elements:

When an element is covered or overlaid by another element, it becomes invisible and unclickable.

from selenium import webdriver
from selenium.common.exceptions import ElementNotVisibleException

driver = webdriver.Chrome()
try:
    driver.get("https://example.com")
    overlayed_element = driver.find_element_by_id("overlayed-element")
    overlayed_element.click()  # Raises ElementNotVisibleException
except ElementNotVisibleException as e:
    print(f"ElementNotVisibleException: {e}")
finally:
    driver.quit()
Enter fullscreen mode Exit fullscreen mode

Element Outside the Viewport:

Elements located outside the visible area of the browser window can't be interacted with.

from selenium import webdriver
from selenium.common.exceptions import ElementNotVisibleException

driver = webdriver.Chrome()
try:
    driver.get("https://example.com")
    offscreen_element = driver.find_element_by_id("offscreen-element")
    offscreen_element.click()  # Raises ElementNotVisibleException
except ElementNotVisibleException as e:
    print(f"ElementNotVisibleException: {e}")
finally:
    driver.quit()
Enter fullscreen mode Exit fullscreen mode

Element Hidden by User Actions:

In some cases, user interactions like clicking on a button can hide an element that was previously visible.

from selenium import webdriver
from selenium.common.exceptions import ElementNotVisibleException

driver = webdriver.Chrome()
try:
    driver.get("https://example.com")
    show_hide_button = driver.find_element_by_id("show-hide-button")
    show_hide_button.click()  # Hides the element
    hidden_element = driver.find_element_by_id("hidden-element")
    hidden_element.click()  # Raises ElementNotVisibleException
except ElementNotVisibleException as e:
    print(f"ElementNotVisibleException: {e}")
finally:
    driver.quit()
Enter fullscreen mode Exit fullscreen mode

To handle ElementNotVisibleException, you should ensure that the element is in a visible state before interacting with it. This may involve waiting for elements to become visible using explicit waits, adjusting CSS properties, or rethinking your test logic to accommodate element visibility changes.

Top comments (0)