We use cookies to analyze site traffic and show personalized ads. You can accept all cookies or decline personalized advertising.
Learn more in our Privacy Policy.
Determine if an element is actually visible to the user, not just present in the DOM.
In Selenium, finding an element in the DOM is not the same as the user actually seeing it on the screen.isDisplayed() helps you answer: "Can a real user see this element?"
isDisplayed() returns:
true when the element is visibly rendered to the user.false when the element exists in the DOM but is visually hidden.true<button> or <p>)opacity > 0falsedisplay: nonevisibility: hiddendisplay: none)findElement(...) works even if the element is hidden. It only checks if it's in the DOM.isDisplayed() when the test cares about what the user can actually see.isDisplayed() with explicit waits when you expect visibility to change (e.g., after clicking a button).Imagine a simple practice page where you can show/hide a "Secret Message" using a toggle button.
On the page, you see:
id="toggle-message-btn"id="secret-status"id="secret-message"Current state: Visible
🎉 You found me!
Interact with the toggle button directly to see how Selenium would check visibility using isDisplayed()!
As a user, you would:
🎉 You found me! (it starts visible).Current state: Hidden.Show Message.Current state: Visible.isDisplayed() on the secret message.isDisplayed().123456789101112WebElement toggleButton = driver.findElement(By.id("toggle-message-btn"));
WebElement message = driver.findElement(By.id("secret-message"));
toggleButton.click();
if (message.isDisplayed()) {
System.out.println("Message is VISIBLE: " + message.getText());
} else {
System.out.println("Message is HIDDEN");
}
driver.quit();true if visible, false if hidden.isDisplayed() to print a clear, human-readable message about the current visibility state.findElement(...) will locate the secret message even when it's hidden. Use isDisplayed() when you care about what the user can see, not just what exists in the DOM.
Always check for visibility (or wait until visible) before clicking or typing. This reduces ElementNotInteractableException and makes your tests behave more like real users.
Don't use isDisplayed() to check if an element exists. If the element might not be in the DOM, you should first handle that with findElements().isEmpty() or proper waits.