</>

Technology

Selenium

Difficulty

Beginner

Interview Question

How can we check if an element is getting displayed on a web page?

Use the isDisplayed() method on a WebElement to check if it is visible on the page.

Answer

Use the isDisplayed() method on a WebElement to check if it is visible on the web page.

Basic usage:

Java
WebElement element = driver.findElement(By.id("errorMessage"));
boolean isVisible = element.isDisplayed();
System.out.println("Is displayed: " + isVisible);

With assertion:

Java
WebElement successBanner = driver.findElement(By.id("successBanner"));
Assert.assertTrue(successBanner.isDisplayed(), "Success banner should be visible");

Check visibility with wait:

Java
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("loader")));
boolean visible = driver.findElement(By.id("loader")).isDisplayed();

isDisplayed() vs isEnabled() vs isSelected():

MethodWhat it checks
isDisplayed()Element is visible on screen (not hidden)
isEnabled()Element is enabled for interaction (not disabled)
isSelected()Element is selected (checkboxes, radio buttons, options)

Note: isDisplayed() returns false for elements with display:none or visibility:hidden. It still returns true for off-screen elements (scrolled out of view).

Follow AutomateQA

Related Topics