In Selenium automation testing, it is important to verify whether a web element is visible on the screen before performing any action like clicking or entering text. Elements that are both displayed and enabled are considered clickable on a web page.
Selenium provides the isDisplayed() method in Java to check if a particular element is visible to the user. This method helps ensure that the element is present in the DOM and not hidden with CSS properties such as display:none or visibility:hidden.
In real-time testing scenarios, you often need to confirm that a web element is displayed before clicking on it. This prevents errors such as ElementNotVisibleException or ElementNotInteractableException.
By using the isDisplayed() method, you can make your Selenium test scripts more reliable and avoid unnecessary test failures.
Below is a simple Selenium WebDriver Java example to check if an element is displayed on the screen:
✅ Example in Java:
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class CheckElementDisplayed {
public static void main(String[] args) {
// Setup ChromeDriver
WebDriver driver = new ChromeDriver();
driver.get(“https://example.com”); // Replace with your test URL
// Locate the element
WebElement element = driver.findElement(By.id("myElement"));
// Check if element is displayed
if (element.isDisplayed()) {
System.out.println("✅ Element is visible on the screen.");
} else {
System.out.println("❌ Element is not visible on the screen.");
}
// Close browser
driver.quit();
}
}