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:
- Use explicit waits: Implement explicit waits to wait for the element to become visible before interacting with it.
- Scroll into view: Scroll the element into view before interacting with it to make it visible.
- Check for overlay elements: Verify if there are any overlay elements on the page that might be hiding the target element.
- Conditional visibility check: If the element's visibility depends on certain conditions, ensure those conditions are met before interacting with it.
- 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 :
- Selenium WebDriver API
- Selenium WebDriver Setup
- Selenium Maven Dependancy
- Selenium Locators
- Selenium with Maven
- Selenium Page Object Model (POM)
- Selenium TestNG
- Selenium Handling Alerts
- Selenium Handling Frames and Windows
- Selenium Waits
- Selenium Cross-browser Testing
- Selenium Reporting
- Selenium Stale Element Exception
- TestNg Class Version Error
- Selenium IDE Download & Installation on Chrome
- Selenium IDE Download & Installation on Microsoft Edge
- Selenium IDE Download & Installation on Mozilla Firefox
- Selenium vs Cypress vs Puppeteer
- Selenium Interview Questions
Selenium Tutorials
Cypress Tutorials
Cypress Archives
- Cypress Installation Errors
- How to Install Cypress
- Cypress Uncaught Exception Handling
- Cypress Automation Examples
- Cypress Automation Tool Interview Questions
- Cypress File Upload Examples
- Error 509 Bandwidth Exceeded
- Cypress Commands
- Cypress Custom Commands
- Handling Cypress Tokens & LocalStorage
- Handling Cypress Multitabs
- Cypress Parallelization
- Cypress waits
- Cypress Still Waiting Error
- Cypress Test Run Errors
- Cypress vs Selenium vs Puppeteer
- Cypress vs Selenium
- Cypress vs Puppeteer