When an element appears in one environment but not in another, it’s important to verify its visibility before performing any operations on it. In Selenium, you can check whether an element is displayed using the isDisplayed() method.
To ensure that all elements have loaded before checking their visibility, it’s best to use explicit waits. This helps Selenium wait until the target element is present and visible on the page.
Unlike some other interactions, isDisplayed() in Selenium does not throw an exception if the element exists in the DOM—it simply returns true if the element is visible or false if it’s hidden. However, if the element is not present in the DOM, trying to locate it will throw a NoSuchElementException. Therefore, it’s a good practice to first ensure the element exists and then check visibility before interacting with it. This helps avoid runtime errors such as NoSuchElementException or ElementNotInteractableException.
Below is an example demonstrating how to verify whether an element is displayed in Selenium using Java, C#, and JavaScript.
Example 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 CheckElementVisibility {
public static void main(String[] args) {
WebDriver driver = new ChromeDriver();
driver.get("https://example.com");
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
try {
WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("myElement")));
if (element.isDisplayed()) {
System.out.println("Element is visible and ready to interact with.");
}
} catch (Exception e) {
System.out.println("Element is not visible.");
} finally {
driver.quit();
}
}
}
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Support.UI;
using System;
class CheckElementVisibility
{
static void Main()
{
IWebDriver driver = new ChromeDriver();
driver.Navigate().GoToUrl("https://example.com");
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
try
{
IWebElement element = wait.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementIsVisible(By.Id("myElement")));
if (element.Displayed)
{
Console.WriteLine("Element is visible and ready to interact with.");
}
}
catch (WebDriverTimeoutException)
{
Console.WriteLine("Element is not visible.");
}
finally
{
driver.Quit();
}
}
}
const { Builder, By, until } = require('selenium-webdriver');
(async function checkElementVisibility() {
let driver = await new Builder().forBrowser('chrome').build();
try {
await driver.get('https://example.com');
let element = await driver.wait(
until.elementIsVisible(driver.findElement(By.id('myElement'))),
10000
);
let isDisplayed = await element.isDisplayed();
if (isDisplayed) {
console.log("Element is visible and ready to interact with.");
}
} catch (err) {
console.log("Element is not visible.");
} finally {
await driver.quit();
}
})();