Hero Background

Next-Gen App & Browser Testing Cloud

Trusted by 2 Mn+ QAs & Devs to accelerate their release cycles

Next-Gen App & Browser Testing Cloud

How can you find whether an element is displayed on the screen using Selenium?

You can find whether an element is displayed on screen using the isDisplayed() method of the WebElement interface. It returns a boolean: true if the element is visible to the user and false if it is present in the DOM but hidden. If the element is not present at all, Selenium throws a NoSuchElementException, so the call is usually wrapped in a try-catch or paired with an explicit wait.

Understanding the isDisplayed() Method in Selenium

When you call isDisplayed(), Selenium inspects the element's rendered state in the browser, not just its presence in the DOM. It considers an element hidden if any of these are true: the CSS is display:none, the CSS is visibility:hidden, the opacity is 0, or the element has zero width and height. In every other case the element is treated as displayed and the method returns true.

This makes isDisplayed() the standard way to confirm that a button, link, message, or field is actually visible before you click it or type into it. Reliable WebDriver scripts check visibility first so they fail with a clear reason instead of interacting with an element the user cannot see.

Checking Element Visibility with isDisplayed()

The safest pattern locates the element, then checks visibility inside a try-catch so a missing element does not crash the test. The Java example below verifies a search box is visible before typing into it.

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.NoSuchElementException;

// Assuming 'driver' is an initialized WebDriver instance
try {
    WebElement searchBox = driver.findElement(By.name("q"));
    if (searchBox.isDisplayed()) {
        System.out.println("Element is displayed");
        searchBox.sendKeys("Selenium");
    } else {
        System.out.println("Element exists but is hidden");
    }
} catch (NoSuchElementException e) {
    System.out.println("Element is not present in the DOM");
}

The try-catch is essential: isDisplayed() only returns false for a hidden-but-present element. If the element is completely absent, findElement() throws NoSuchElementException before isDisplayed() runs.

Using Explicit Waits for Dynamic Elements

On pages that load content asynchronously, an element may not be visible the instant the script reaches it. Instead of a bare isDisplayed() call, use WebDriverWait with ExpectedConditions.visibilityOfElementLocated, which polls until the element becomes visible or the timeout expires.

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.openqa.selenium.support.ui.ExpectedConditions;
import java.time.Duration;

WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
WebElement element = wait.until(
    ExpectedConditions.visibilityOfElementLocated(By.id("myElement"))
);
System.out.println("Element is now displayed: " + element.isDisplayed());

To learn more about the different wait strategies, see the types of waits available in WebDriver.

Doing the Same Check in Python

The visibility check exists in every Selenium binding. In Python the method is is_displayed() and the pattern is identical.

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.common.exceptions import NoSuchElementException

driver = webdriver.Chrome()
driver.get("https://www.example.com")

try:
    element = driver.find_element(By.ID, "myElement")
    if element.is_displayed():
        print("Element is displayed")
    else:
        print("Element exists but is hidden")
except NoSuchElementException:
    print("Element is not present in the DOM")

driver.quit()

isDisplayed() vs isEnabled() vs isSelected()

Selenium offers three related boolean state checks that are easy to mix up:

  • isDisplayed(): Is the element visible on screen?
  • isEnabled(): Is the element active and interactable, for example a button that is not disabled?
  • isSelected(): Is a checkbox, radio button, or dropdown option currently selected?

A robust test often combines them, for example confirming a submit button is both displayed and enabled before clicking.

Common Mistakes and Troubleshooting

  • Expecting false for a missing element: A completely absent element throws NoSuchElementException, not false. Always wrap the call in try-catch.
  • Using outdated find methods: find_element_by_id and similar helpers were removed in Selenium 4. Use find_element(By.ID, "...") instead.
  • Skipping waits on dynamic pages: Checking visibility before content loads gives false negatives. Use an explicit wait.
  • Confusing presence with visibility: An element can be in the DOM yet hidden by CSS. isDisplayed() distinguishes the two.
  • Checking non-display for absence: To assert an element is gone, use invisibilityOfElementLocated rather than a negated isDisplayed().

Verifying Visibility Across Real Browsers and Devices

Element visibility can differ between browsers and screen sizes: an element that renders on desktop Chrome may be pushed off-screen or hidden by a responsive layout on Safari or a mobile viewport. That is why isDisplayed() assertions should be validated across many environments, not just one local browser. TestMu AI lets you run your Selenium suites on a cloud of 3000+ real browsers and operating systems, so a visibility check that passes locally is confirmed on the exact browser and device combinations your users have. Running automation testing at this scale catches rendering and layout defects that a single-browser run would miss.

Conclusion

The isDisplayed() method is the standard way to check whether an element is visible on screen in Selenium. Remember that it returns false only for hidden-but-present elements and throws an exception for missing ones, so pair it with try-catch or an explicit wait for reliability. Combine it with isEnabled() and isSelected() for complete state checks, and validate across real browsers to catch layout-specific issues. To keep building your locator skills, see how to locate elements on a web page.

Frequently Asked Questions

Does isDisplayed() return false or throw an exception when the element is missing?

isDisplayed() returns false only when the element exists in the DOM but is hidden. If the element is not present at all, findElement() throws a NoSuchElementException before isDisplayed() ever runs, so you must wrap the call in a try-catch or use an explicit wait.

What is the difference between isDisplayed(), isEnabled(), and isSelected()?

isDisplayed() checks whether an element is visible on screen. isEnabled() checks whether it is active and can be interacted with, such as a non-disabled button. isSelected() checks whether a checkbox, radio button, or dropdown option is currently selected.

How does Selenium decide if an element is displayed?

Selenium checks CSS properties and dimensions. An element is treated as not displayed if it has display:none, visibility:hidden, opacity:0, or zero width and height. In every other case the element is considered visible and isDisplayed() returns true.

How can you wait until an element becomes visible in Selenium?

Use WebDriverWait with ExpectedConditions.visibilityOfElementLocated(locator). Selenium polls the DOM until the element is present and visible or the timeout expires, which is far more reliable than a bare isDisplayed() call on a dynamic, asynchronously loaded page.

How do you check that an element is NOT displayed?

Use ExpectedConditions.invisibilityOfElementLocated(locator) with WebDriverWait, which returns true when the element is hidden or absent. Relying on isDisplayed() alone is risky because a missing element throws an exception instead of returning false.

Can isDisplayed() be used in languages other than Java?

Yes. The visibility check exists in every Selenium binding: is_displayed() in Python, isDisplayed() in Java and JavaScript, and IsDisplayed in C#. The behavior and rules for what counts as displayed are the same across all languages.

Related Questions

Test Your Website on 3000+ Browsers

Get 100 minutes of automation test minutes FREE!!

Test Now...

KaneAI - Testing Assistant

World’s first AI-Native E2E testing agent.

...

TestMu AI forEnterprise

Get access to solutions built on Enterprise
grade security, privacy, & compliance

  • Advanced access controls
  • Advanced data retention rules
  • Advanced Local Testing
  • Premium Support options
  • Early access to beta features
  • Private Slack Channel
  • Unlimited Manual Accessibility DevTools Tests