Turn your manual testers into automation experts! Request a DemoStart testRigor Free

Overcoming Element Not Visible Exception: Best Practices for Selenium WebDriver

Element Not Visible Exception in Selenium

org.openqa.selenium.ElementNotVisibleException: Understanding and Handling ElementNotVisibleException in Selenium with Java.

Selenium WebDriver is a widely used tool for browser automation across various programming languages, including Java. One common exception developers may encounter when working with Selenium is ElementNotVisibleException. This article provides a comprehensive understanding of this common exception, its causes, and how to handle it effectively. Additionally, it includes a Java code example to demonstrate handling the exception.

What is ElementNotVisibleException?

ElementNotVisibleException is an exception thrown by Selenium WebDriver when an element is present in the DOM, but it is not visible to the user, meaning that the user cannot see or interact with it, such as clicking or sending keys. This exception indicates that the element you are trying to interact with is either hidden, obscured, or in a state that prevents user visibility.

Primary Causes of Element Not Visible Exception

The most frequent reasons for encountering this exception include:
  • Element visibility: The target element might be hidden, obscured by another element, or outside the viewport, making it non-visible.
  • Element state: The element could be in a collapsed or minimized state, preventing user visibility. If that is the case, it means that the element is designed to be non-visible to mimic user interactions.
  • Timing issues: The element may be temporarily non-visible, particularly in dynamic web applications with AJAX and JavaScript. In this case, Selenium might be attempting to interact with the element before it becomes visible, resulting in the exception.

Addressing ElementNotVisibleException

To troubleshoot ElementNotVisibleException effectively, follow these steps:
  1. Inspect the element: Check the element's visibility and state (collapsed or expanded) in the browser's developer tools.
  2. Wait for the element to become visible: Implement explicit (recommended) or implicit waits to provide sufficient time for the element to become visible before attempting to perform actions on it.

Code example:

import org.openqa.selenium.By;
import org.openqa.selenium.ElementNotVisibleException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class ElementNotVisibleExceptionExample {
  public static void main(String[] args) {
    System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
    WebDriver driver = new ChromeDriver();
    WebDriverWait wait = new WebDriverWait(driver, 10); // 10-second explicit wait

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

      // Wait until the element is visible
      WebElement button = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("buttonId")));

      // Perform actions on the element
      button.click();
    } catch (ElementNotVisibleException e) {
      System.out.println("Element not visible: " + e.getMessage());
    } finally {
     driver.quit();
    }
  }
}

Here, the code sample demonstrates handling ElementNotVisibleException in a Selenium Java test. It imports required classes, initializes WebDriver and WebDriverWait, and uses a try-catch block to navigate to a webpage, wait for an element to become visible, and perform actions on the element. If ElementNotVisibleException is thrown, the error message is printed, and the browser and WebDriver session are terminated in the finally block.

This example showcases handling ElementNotVisibleException and waiting for elements to become visible before interacting with them in a Selenium test. By following the recommended practices and using the provided code example as a guide, you can significantly reduce the occurrence of ElementNotVisibleException and improve the stability of their automated tests.

Why ElementNotVisibleException is so common

You can read why Selenium is not an adequate solution for modern websites here. Smart tools such as testRigor don't depend on any locators, and as a QA professional, you can forget about the underlying infrastructure - and focus on your test having the best assertions and testing for the right things. You can simply switch to testRigor to forever escape element exception issues like the one described in this article.

By using intelligent testing tools such as testRigor, you can avoid common issues like ElementNotVisibleException, as it is designed to handle dynamic web applications more effectively. testRigor does not rely solely on locators and can better adapt to changes in the application's user interface. This adaptability allows you to focus on creating and maintaining high-quality test cases, ensuring the desired outcomes and stability of your automated tests.

Related Articles

Selenium with C# Cheat Sheet

Driver Initialization Chrome IWebDriver driver = new ChromeDriver(); Firefox IWebDriver driver = new FirefoxDriver(); Edge ...

Selenium with JS (JavaScript) Cheat Sheet

Driver Initialization Chrome const driver = await new Builder().forBrowser('chrome').build(); Firefox const driver = await new ...

Selenium with Ruby Cheat Sheet

Driver Initialization Chrome driver = Selenium::WebDriver.for :chrome Firefox driver = Selenium::WebDriver.for :firefox Edge ...