Java Selenium ElementNotVisibleException - Hidden Elements

Selenium is a powerful tool for automating web browsers, enabling testers and developers to execute various web automation tasks. However, during test script execution, you may encounter exceptions like "ElementNotVisibleException," which occurs when an attempt is made to interact with a web element that is hidden or not visible on the web page.

Understanding ElementNotVisibleException

The ElementNotVisibleException is thrown when an element is present in the DOM (Document Object Model) of the web page but is not currently visible to the user. This usually happens when the element is hidden by CSS styles, overlays, or other page elements.

Common Causes

Several common causes can lead to the ElementNotVisibleException:

  • Hidden by CSS: The element may be hidden using CSS styles like "display: none" or "visibility: hidden".
  • Overlay elements: The element might be covered by other overlapping elements, like modal dialogs or tooltips.
  • Conditional visibility: The element's visibility depends on certain conditions, and it may not always be visible.
  • Delayed visibility: The element might become visible after a certain time due to animations or delays.
  • Dynamic content changes: The page content dynamically changes, causing the element to become hidden or invisible.

Handling ElementNotVisibleException

Handling the ElementNotVisibleException requires adopting some best practices:

  1. Use explicit waits: Implement explicit waits to wait for the element to become visible before interacting with it.
  2. Scroll into view: Scroll the element into view before interacting with it to make it visible.
  3. Check for overlay elements: Verify if there are any overlay elements on the page that might be hiding the target element.
  4. Conditional visibility check: If the element's visibility depends on certain conditions, ensure those conditions are met before interacting with it.
  5. Wait for animations and delays: If the element becomes visible after animations or delays, wait for them to complete before interacting with the element.

Example Code

Here's an example of how to handle ElementNotVisibleException in Java Selenium:


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

public class ElementNotVisibleExample {
public static void main(String[] args) {
WebDriver driver = new ChromeDriver();
driver.get("https://techoral.com");


    try {
        WebElement element = new WebDriverWait(driver, 10)
            .until(ExpectedConditions.visibilityOfElementLocated(By.id("someElementId")));
        // Scroll element into view
        Actions actions = new Actions(driver);
        actions.moveToElement(element).perform();
        // Perform some actions on the element
        element.click();
    } catch (org.openqa.selenium.ElementNotVisibleException e) {
        // Handle the exception or report the failure
    }

    driver.quit();
}
}

Conclusion

The ElementNotVisibleException is a common exception faced during web automation using Java Selenium. Understanding its causes and implementing best practices to handle it can lead to more robust and reliable test scripts. By using explicit waits, scrolling into view, and checking for overlay elements, you can effectively handle situations where an element is hidden or not visible, ensuring smoother and more successful web automation.





Read Next :






Subscribe to Our Newsletter

Get the latest updates and exclusive content delivered to your inbox!