Fix an ElementNotVisibleException

The ElementNotVisibleException, occurs when you attempt to interact with an element on a web page that is present in the DOM (Document Object Model) but not visible or interactable to the user. Several reasons can lead to this exception:

  1. CSS Properties: The element's CSS properties, such as "display: none" or "visibility: hidden," may be set in a way that makes it invisible on the web page. This often happens with elements that are initially hidden and become visible only after specific user interactions or events.

  2. Hidden Elements: Some elements, such as certain pop-ups or dropdown menus, may be hidden by default and become visible only when triggered by user actions like clicking or hovering. Attempting to interact with such elements before they are made visible can result in the ElementNotVisibleException.

  3. Element Overlapping: Another element may be covering the element you are trying to interact with, making it effectively invisible or inaccessible. This can happen with elements that share the same screen space or have overlapping positions.

  4. Dynamic Content Loading: The web page might be using dynamic content loading techniques, such as AJAX, which means that certain elements might not be visible until they are loaded asynchronously. If Selenium tries to interact with the element before it becomes visible, it can lead to this exception.

To handle the ElementNotVisibleException effectively, consider the following strategies:

  • Ensure that the element is genuinely supposed to be visible at the time you are trying to interact with it. Verify the element's visibility status in the HTML/CSS or through browser developer tools.

  • Use explicit waits with the WebDriverWait class to wait for the element to become visible before interacting with it. You can specify conditions like ExpectedConditions.visibilityOfElementLocated.

  • If the element is expected to become visible after a user action, simulate that action (e.g., clicking a button) before attempting to interact with the element.

  • Check for overlapping elements that might be preventing access to the element you want to interact with and ensure proper ordering or z-index settings.

  • Handle dynamic content loading scenarios by waiting for the element to appear using explicit waits.

By addressing these issues and implementing proper synchronization and validation mechanisms, you can avoid or handle the ElementNotVisibleException in your Selenium test automation effectively.

Last updated