Skip to content
CodeKruPro

isDisplayed() - Check Element Visibility

32/70

isDisplayed() - Check Visibility

isDisplayed() returns true if an element is visible on the page. Elements with visibility: hidden or display: none return false.

Task:

  1. Navigate to https://testkru.com/Elements/TextFields
  2. Check if firstName (visible) is displayed
  3. Check if invisibleField (hidden with visibility:hidden) is displayed

Expected output:

firstName displayed: true
invisibleField displayed: false
Read full tutorial on Codekru

About this lesson

isDisplayed in Selenium Java: check whether an element is visible

Selenium Java AssignmentsIntermediateElement States

How it works

isDisplayed() returns true if the element would be visible to a user. It returns false for an element hidden with visibility: hidden, with display: none, with opacity: 0, or inside a hidden parent. On Text Fields, invisibleField is in the DOM, so it can be found, but it is not displayed.

Being displayed is not the same as being on screen. An element further down the page, below the current viewport, still returns true. Visibility here is about CSS, not scrolling.

isDisplayed() needs an element first. If the element is not in the DOM at all, findElement() throws NoSuchElementException before isDisplayed() is ever called. To test "not present or not visible" safely, use findElements() and check the list.

About the assignment

Text Fields has ordinary inputs such as firstName and one input, invisibleField, styled visibility: hidden. You check isDisplayed() on each to see that both exist in the DOM but only one is visible.

What you will do

  1. Find the firstName field and the invisibleField field by id.
  2. Call isDisplayed() on each.
  3. Print one line per field in the order and format the task shows, field id first.

Common mistakes

  • Using isDisplayed() to check absence

    For an element that is not in the DOM, findElement() throws NoSuchElementException, so the false you expected never comes back.

  • Reading the style attribute instead

    Checking the inline style misses hiding done by stylesheets and parent elements. isDisplayed() uses the computed result.

  • Calling it in a loop to wait

    Polling isDisplayed() by hand with Thread.sleep() is slow and flaky. ExpectedConditions.visibilityOfElementLocated() does this properly, as lesson 36 shows.

In a real test suite

Visibility checks back assertions such as "the error message appears after an invalid submit" or "the spinner is gone". Most of these are done through waits, which call the same visibility logic until it returns the answer you need.

Lesson 31 read a dropdown's choice. The next two lessons cover the other two state checks: isEnabled() and isSelected().

To try one of these calls on any page without a task, open the online Selenium compiler for Java.

Starter code

The editor above opens with this code. Fill in the TODO lines, then run and submit it.

Main.java
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;

public class Main {
    public static void main(String[] args) throws Exception {
        WebDriver driver = new ChromeDriver();
        driver.get("https://testkru.com/Elements/TextFields");
        
        // TODO: Check isDisplayed() for firstName and invisibleField
        
        driver.quit();
    }
}
Read the full tutorial