In Selenium, an ElementNotVisibleException occurs when an element is present in the DOM (Document Object Model) but is not visible on the UI (User Interface).
To avoid this exception:
- Explicit Wait allows you to wait until a specific condition (like visibility) is met before interacting with the element.
- Use Explicit Waits instead of Implicit Waits.
- Avoid using
Thread.sleep()or implicit waits (driver.Manage().Timeouts()) to wait for elements, as they may not guarantee visibility and can still cause exceptions.
Here is an example script below to handle ElementNotVisibleException in selenium with Java, C# and JavaScript:
Selenium Script
-
Java
-
C#
-
JavaScript
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.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import java.time.Duration;
public class HandleElementNotVisible {
public static void main(String[] args) {
// Set path to your ChromeDriver if not in system PATH
System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
try {
driver.get("https://example.com");
// Explicit Wait for up to 10 seconds
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
// Wait until the element is visible
WebElement element =wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("elem_Id")));
// Perform action on the element
element.click();
System.out.println("Element clicked successfully!");
} catch (Exception e) {
System.out.println("Element was not visible or not found: " + e.getMessage());
} finally {
driver.quit();
}
}
}
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Support.UI;
using System;
class HandleElementNotVisible
{
static void Main()
{
IWebDriver driver = new ChromeDriver();
driver.Navigate().GoToUrl("https://example.com");
try
{
// Define Explicit Wait
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
// Wait until the element is visible
IWebElement element = wait.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementIsVisible(By.Id("Elem_id")));
// Now perform action on the element
element.Click();
Console.WriteLine("Element clicked successfully!");
}
catch (WebDriverTimeoutException)
{
Console.WriteLine("Element was not visible within the specified time.");
}
finally
{
driver.Quit();
}
}
}
const { Builder, By, until } = require(‘selenium-webdriver’);
(async function handleElementNotVisible() {
// Initialize ChromeDriver
let driver = await new Builder().forBrowser(‘chrome’).build();
try {
await driver.get('https://example.com');
// wait up to 10 seconds until element is visible
let element = await driver.wait(until.elementIsVisible(
driver.findElement(By.id('elem_Id'))),
10000 // timeout in milliseconds
);
// Perform action on the element
await element.click();
console.log('Element clicked successfully!');
} catch (error) {
if (error.name === 'TimeoutError') {
console.log('Element was not visible ');
} else {
console.log('An error occurred:', error.message);
}
} finally {
// Quit driver
await driver.quit();
}
})();