isDisplayed() in Selenium
isDisplayed() is a Selenium WebDriver method used to determine whether a web element is currently displayed on a web page. It returns a boolean value: true when the element is displayed and false when the element is not displayed.
The method is commonly used to verify the visibility of buttons, text fields, labels, images, menus, links, messages, forms, popups, error messages, and other web elements during automated testing.
JustAcademy's Selenium Automation Testing course covers Selenium WebDriver, WebElements, locators, TestNG, automation frameworks, Page Object Model, dynamic elements, cross-browser testing, and real-time automation projects. You can learn more through the Selenium Training Course and register for a Selenium Course Demo.
1. What is isDisplayed() in Selenium?
isDisplayed() is a method of the Selenium WebElement interface. It checks whether a particular web element is displayed to the user according to WebDriver's visibility rules.
The method returns a boolean value.
boolean result = element.isDisplayed();
If the element is displayed, the result is:
true
If the element is not displayed, the result is:
false
2. Why isDisplayed() Is Important
In web automation, locating an element does not always mean that the element is currently visible. Modern web applications frequently hide and display elements based on user actions, application state, permissions, validation, AJAX requests, menus, dialogs, and responsive layouts.
- Verify that an element is visible.
- Verify that a button is displayed.
- Verify that a login form is visible.
- Verify that an error message appears.
- Verify that a success message is displayed.
- Verify that a popup is visible.
- Verify that a menu is displayed.
- Verify that an image is visible.
- Verify that a field is displayed before interacting with it.
- Validate dynamic UI behavior.
3. Basic Syntax of isDisplayed()
The basic syntax is:
element.isDisplayed();
Usually, the result is stored in a boolean variable.
boolean displayed = element.isDisplayed();
4. Simple isDisplayed() Example
WebElement loginButton = driver.findElement(By.id("login"));
boolean displayed = loginButton.isDisplayed();
System.out.println("Login Button Displayed: " + displayed);
If the login button is visible, Selenium returns true.
5. Return Type of isDisplayed()
The return type of isDisplayed() is boolean.
| Result | Meaning |
| true | The element is displayed. |
| false | The element is not displayed. |
6. Checking Whether a Button Is Displayed
WebElement button = driver.findElement(By.id("submit"));
if (button.isDisplayed()) {
System.out.println("Submit button is displayed");
}
7. Checking Whether an Element Is Not Displayed
WebElement errorMessage = driver.findElement(By.id("error"));
if (!errorMessage.isDisplayed()) {
System.out.println("Error message is not displayed");
}
8. isDisplayed() with if-else
WebElement element = driver.findElement(By.id("message"));
if (element.isDisplayed()) {
System.out.println("Element is visible");
} else {
System.out.println("Element is not visible");
}
9. isDisplayed() with Boolean Variable
WebElement logo = driver.findElement(By.id("logo"));
boolean isVisible = logo.isDisplayed();
System.out.println("Logo visible: " + isVisible);
10. isDisplayed() with TestNG
TestNG assertions can be used to verify that an element is displayed.
WebElement loginButton = driver.findElement(By.id("login"));
Assert.assertTrue(loginButton.isDisplayed());
This test passes when the login button is displayed.
11. Verify an Element Is Not Displayed with TestNG
WebElement errorMessage = driver.findElement(By.id("error"));
Assert.assertFalse(errorMessage.isDisplayed());
12. isDisplayed() vs isEnabled()
isDisplayed() and isEnabled() check different properties of a web element.
| Method | Purpose | Return Type |
| isDisplayed() | Checks whether the element is displayed. | boolean |
| isEnabled() | Checks whether the element is enabled. | boolean |
An element can be displayed but disabled.
boolean visible = button.isDisplayed();
boolean enabled = button.isEnabled();
13. isDisplayed() vs isSelected()
isSelected() is generally used for checkboxes, radio buttons, and options, while isDisplayed() checks visibility.
| Method | Checks |
| isDisplayed() | Visibility/displayed state |
| isSelected() | Selected state |
| isEnabled() | Enabled state |
14. isDisplayed() vs getText()
isDisplayed() returns whether an element is displayed, while getText() retrieves the visible text associated with the element.
WebElement message = driver.findElement(By.id("message"));
boolean displayed = message.isDisplayed();
String text = message.getText();
System.out.println("Displayed: " + displayed);
System.out.println("Text: " + text);
15. isDisplayed() vs getAttribute()
isDisplayed() checks displayed state, whereas getAttribute() retrieves an attribute or property-related value from the element.
WebElement input = driver.findElement(By.id("username"));
boolean displayed = input.isDisplayed();
String value = input.getAttribute("value");
System.out.println("Displayed: " + displayed);
System.out.println("Value: " + value);
16. Checking a Login Form
Before entering credentials, a test can verify that the login form is displayed.
WebElement username = driver.findElement(By.id("username"));
WebElement password = driver.findElement(By.id("password"));
WebElement loginButton = driver.findElement(By.id("login"));
Assert.assertTrue(username.isDisplayed());
Assert.assertTrue(password.isDisplayed());
Assert.assertTrue(loginButton.isDisplayed());
17. Checking a Logo
WebElement logo = driver.findElement(By.cssSelector(".logo"));
if (logo.isDisplayed()) {
System.out.println("Logo is displayed");
} else {
System.out.println("Logo is not displayed");
}
18. Checking a Navigation Menu
WebElement menu = driver.findElement(By.id("menu"));
Assert.assertTrue(menu.isDisplayed());
19. Checking an Image
WebElement productImage = driver.findElement(
By.cssSelector(".product-image")
);
if (productImage.isDisplayed()) {
System.out.println("Product image is visible");
}
20. Checking a Text Field
WebElement username = driver.findElement(By.id("username"));
if (username.isDisplayed()) {
username.sendKeys("testuser");
}
Checking visibility before interaction can make the test flow easier to understand, although for dynamic applications an appropriate explicit wait is usually better than an immediate visibility check.
21. Checking a Submit Button
WebElement submit = driver.findElement(By.id("submit"));
boolean visible = submit.isDisplayed();
System.out.println("Submit button visible: " + visible);
22. Checking an Error Message
Error messages are often displayed only after invalid data is submitted.
WebElement error = driver.findElement(By.id("error"));
if (error.isDisplayed()) {
System.out.println("Validation error is displayed");
}
23. Checking a Success Message
WebElement success = driver.findElement(By.id("success"));
Assert.assertTrue(success.isDisplayed());
24. Checking a Popup
WebElement popup = driver.findElement(By.id("popup"));
if (popup.isDisplayed()) {
System.out.println("Popup is displayed");
}
25. Checking a Hidden Element
Some web applications contain elements in the DOM that are not currently displayed.
WebElement element = driver.findElement(By.id("hiddenElement"));
boolean visible = element.isDisplayed();
System.out.println("Visible: " + visible);
The element can exist in the DOM while its displayed state is false.
26. Important Difference Between DOM Presence and Visibility
Finding an element and displaying an element are different concepts. findElement() attempts to locate an element in the DOM, while isDisplayed() checks whether the located element is displayed.
WebElement element = driver.findElement(By.id("message"));
boolean displayed = element.isDisplayed();
A successfully located element is not automatically guaranteed to be visible.
27. NoSuchElementException and isDisplayed()
If the element cannot be found, findElement() can throw NoSuchElementException. In that situation, Selenium never reaches the isDisplayed() call.
WebElement element = driver.findElement(By.id("missingElement"));
boolean visible = element.isDisplayed();
For dynamic pages, explicit waits can be used when an element may appear later.
28. isDisplayed() with XPath
WebElement loginButton = driver.findElement(
By.xpath("//button[@id='login']")
);
boolean visible = loginButton.isDisplayed();
System.out.println("Visible: " + visible);
29. isDisplayed() with CSS Selector
WebElement loginButton = driver.findElement(
By.cssSelector("#login")
);
Assert.assertTrue(loginButton.isDisplayed());
30. isDisplayed() with Class Name
WebElement logo = driver.findElement(
By.className("logo")
);
System.out.println(logo.isDisplayed());
31. isDisplayed() with Link
WebElement homeLink = driver.findElement(
By.linkText("Home")
);
if (homeLink.isDisplayed()) {
System.out.println("Home link is visible");
}
32. isDisplayed() with Partial Link Text
WebElement link = driver.findElement(
By.partialLinkText("Learn")
);
Assert.assertTrue(link.isDisplayed());
33. Checking Multiple Elements
When multiple elements need to be checked, Selenium's findElements() method can be useful.
List<WebElement> buttons = driver.findElements(
By.cssSelector("button")
);
for (WebElement button : buttons) {
System.out.println(
"Button displayed: " + button.isDisplayed()
);
}
34. Checking All Visible Buttons
List<WebElement> buttons = driver.findElements(
By.cssSelector("button")
);
for (WebElement button : buttons) {
if (button.isDisplayed()) {
System.out.println("Visible button found");
}
}
35. Counting Displayed Elements
List<WebElement> elements = driver.findElements(
By.cssSelector(".item")
);
int visibleCount = 0;
for (WebElement element : elements) {
if (element.isDisplayed()) {
visibleCount++;
}
}
System.out.println("Visible elements: " + visibleCount);
36. Checking Whether At Least One Element Is Displayed
List<WebElement> elements = driver.findElements(
By.cssSelector(".notification")
);
boolean foundVisible = false;
for (WebElement element : elements) {
if (element.isDisplayed()) {
foundVisible = true;
break;
}
}
System.out.println("Visible notification found: " + foundVisible);
37. isDisplayed() and Explicit Wait
For dynamic applications, an immediate call to isDisplayed() may happen before the element becomes visible. Selenium's explicit wait can wait for a visibility condition.
WebDriverWait wait = new WebDriverWait(
driver,
Duration.ofSeconds(10)
);
WebElement message = wait.until(
ExpectedConditions.visibilityOfElementLocated(
By.id("message")
)
);
System.out.println("Message is visible");
38. visibilityOfElementLocated()
visibilityOfElementLocated() is an ExpectedCondition that waits until an element is located and considered visible.
WebDriverWait wait = new WebDriverWait(
driver,
Duration.ofSeconds(10)
);
WebElement element = wait.until(
ExpectedConditions.visibilityOfElementLocated(
By.id("login")
)
);
39. visibilityOf()
If a WebElement has already been located, visibilityOf() can be used with an explicit wait.
WebElement message = driver.findElement(By.id("message"));
WebDriverWait wait = new WebDriverWait(
driver,
Duration.ofSeconds(10)
);
wait.until(ExpectedConditions.visibilityOf(message));
40. isDisplayed() vs visibilityOfElementLocated()
| Feature | isDisplayed() | visibilityOfElementLocated() |
| Type | WebElement method | ExpectedCondition |
| Purpose | Immediately checks displayed state | Waits for visibility |
| Return | boolean | WebElement |
| Synchronization | No | Yes, when used with WebDriverWait |
41. isDisplayed() Is Not a Wait
One of the most important concepts is that isDisplayed() is an immediate state check. It does not automatically wait for an element to become visible.
boolean visible = element.isDisplayed();
If the application displays the element one second later, the immediate call does not wait for that future state.
42. Dynamic Element Example
Consider a page where a success message appears after clicking a Save button.
driver.findElement(By.id("save")).click();
WebDriverWait wait = new WebDriverWait(
driver,
Duration.ofSeconds(10)
);
WebElement message = wait.until(
ExpectedConditions.visibilityOfElementLocated(
By.id("success")
)
);
Assert.assertTrue(message.isDisplayed());
43. Checking Login Page Visibility
WebDriverWait wait = new WebDriverWait(
driver,
Duration.ofSeconds(10)
);
WebElement username = wait.until(
ExpectedConditions.visibilityOfElementLocated(
By.id("username")
)
);
WebElement password = wait.until(
ExpectedConditions.visibilityOfElementLocated(
By.id("password")
)
);
WebElement loginButton = wait.until(
ExpectedConditions.visibilityOfElementLocated(
By.id("login")
)
);
Assert.assertTrue(username.isDisplayed());
Assert.assertTrue(password.isDisplayed());
Assert.assertTrue(loginButton.isDisplayed());
44. Checking Menu After Clicking Menu Button
driver.findElement(By.id("menuButton")).click();
WebDriverWait wait = new WebDriverWait(
driver,
Duration.ofSeconds(10)
);
WebElement menu = wait.until(
ExpectedConditions.visibilityOfElementLocated(
By.id("menu")
)
);
Assert.assertTrue(menu.isDisplayed());
45. Checking Modal Dialog
driver.findElement(By.id("openModal")).click();
WebDriverWait wait = new WebDriverWait(
driver,
Duration.ofSeconds(10)
);
WebElement modal = wait.until(
ExpectedConditions.visibilityOfElementLocated(
By.id("modal")
)
);
Assert.assertTrue(modal.isDisplayed());
46. Checking Loading Message
Loading indicators are commonly displayed while AJAX or other asynchronous operations are running.
WebElement loader = driver.findElement(By.id("loader"));
if (loader.isDisplayed()) {
System.out.println("Loading is in progress");
}
47. Waiting for Loader to Disappear
When the requirement is to verify that a loading element is no longer displayed, an explicit wait can be used.
WebDriverWait wait = new WebDriverWait(
driver,
Duration.ofSeconds(10)
);
wait.until(
ExpectedConditions.invisibilityOfElementLocated(
By.id("loader")
)
);
System.out.println("Loader is no longer visible");
48. isDisplayed() with Invisibility Check
After waiting for invisibility, an element can be checked again if it is still represented in the DOM.
WebElement loader = driver.findElement(By.id("loader"));
boolean visible = loader.isDisplayed();
System.out.println("Loader visible: " + visible);
49. Checking Dropdown Visibility
WebElement countryDropdown = driver.findElement(
By.id("country")
);
Assert.assertTrue(countryDropdown.isDisplayed());
50. Checking Checkbox Visibility
WebElement terms = driver.findElement(By.id("terms"));
if (terms.isDisplayed()) {
System.out.println("Terms checkbox is visible");
}
51. Checking Radio Button Visibility
WebElement male = driver.findElement(By.id("male"));
Assert.assertTrue(male.isDisplayed());
52. Checking Table Visibility
WebElement table = driver.findElement(By.id("usersTable"));
Assert.assertTrue(table.isDisplayed());
53. Checking Table Rows
List<WebElement> rows = driver.findElements(
By.cssSelector("#usersTable tbody tr")
);
for (WebElement row : rows) {
if (row.isDisplayed()) {
System.out.println("Visible row: " + row.getText());
}
}
54. Checking Product Cards
List<WebElement> products = driver.findElements(
By.cssSelector(".product-card")
);
for (WebElement product : products) {
if (product.isDisplayed()) {
System.out.println("Product card is visible");
}
}
55. Checking Add to Cart Button
WebElement addToCart = driver.findElement(
By.cssSelector(".add-to-cart")
);
if (addToCart.isDisplayed()) {
System.out.println("Add to Cart button is visible");
}
56. Checking Checkout Form
WebElement checkoutForm = driver.findElement(
By.id("checkoutForm")
);
Assert.assertTrue(checkoutForm.isDisplayed());
57. Practical E-Commerce Example
WebElement product = driver.findElement(
By.cssSelector(".product-card")
);
WebElement addToCart = product.findElement(
By.cssSelector(".add-to-cart")
);
if (product.isDisplayed() && addToCart.isDisplayed()) {
addToCart.click();
}
58. Practical Login Example
WebElement username = driver.findElement(By.id("username"));
WebElement password = driver.findElement(By.id("password"));
WebElement login = driver.findElement(By.id("login"));
if (username.isDisplayed() &&
password.isDisplayed() &&
login.isDisplayed()) {
username.sendKeys("testuser");
password.sendKeys("password");
login.click();
}
59. Checking Error Message After Invalid Login
driver.findElement(By.id("login")).click();
WebDriverWait wait = new WebDriverWait(
driver,
Duration.ofSeconds(10)
);
WebElement error = wait.until(
ExpectedConditions.visibilityOfElementLocated(
By.id("loginError")
)
);
Assert.assertTrue(error.isDisplayed());
60. Checking Success Message After Form Submission
driver.findElement(By.id("submit")).click();
WebDriverWait wait = new WebDriverWait(
driver,
Duration.ofSeconds(10)
);
WebElement success = wait.until(
ExpectedConditions.visibilityOfElementLocated(
By.id("successMessage")
)
);
Assert.assertTrue(success.isDisplayed());
61. Checking a Tooltip
driver.findElement(By.id("help")).click();
WebDriverWait wait = new WebDriverWait(
driver,
Duration.ofSeconds(10)
);
WebElement tooltip = wait.until(
ExpectedConditions.visibilityOfElementLocated(
By.id("tooltip")
)
);
Assert.assertTrue(tooltip.isDisplayed());
62. Checking a Notification
WebDriverWait wait = new WebDriverWait(
driver,
Duration.ofSeconds(10)
);
WebElement notification = wait.until(
ExpectedConditions.visibilityOfElementLocated(
By.cssSelector(".notification")
)
);
System.out.println(notification.isDisplayed());
63. isDisplayed() with Page Object Model
In the Page Object Model, visibility checks can be placed inside reusable page methods.
public class LoginPage {
private WebDriver driver;
private By username = By.id("username");
private By password = By.id("password");
private By loginButton = By.id("login");
public LoginPage(WebDriver driver) {
this.driver = driver;
}
public boolean isUsernameDisplayed() {
return driver.findElement(username).isDisplayed();
}
public boolean isPasswordDisplayed() {
return driver.findElement(password).isDisplayed();
}
public boolean isLoginButtonDisplayed() {
return driver.findElement(loginButton).isDisplayed();
}
}
64. Using Page Object Visibility Methods
LoginPage loginPage = new LoginPage(driver);
Assert.assertTrue(loginPage.isUsernameDisplayed());
Assert.assertTrue(loginPage.isPasswordDisplayed());
Assert.assertTrue(loginPage.isLoginButtonDisplayed());
65. Reusable Visibility Method
A generic helper method can be created for visibility checks.
public boolean isElementDisplayed(By locator) {
try {
return driver.findElement(locator).isDisplayed();
} catch (NoSuchElementException e) {
return false;
}
}
This approach can be useful when the test intentionally needs a boolean result instead of an exception when an element is absent.
66. Visibility Helper with WebDriverWait
public boolean waitForElementVisible(By locator) {
try {
WebDriverWait wait = new WebDriverWait(
driver,
Duration.ofSeconds(10)
);
wait.until(
ExpectedConditions.visibilityOfElementLocated(locator)
);
return true;
} catch (TimeoutException e) {
return false;
}
}
67. Handling NoSuchElementException
If an element may not exist at all, calling findElement() directly can throw an exception. A helper method can handle this situation.
try {
WebElement element = driver.findElement(By.id("message"));
if (element.isDisplayed()) {
System.out.println("Message is visible");
}
} catch (NoSuchElementException e) {
System.out.println("Message element does not exist");
}
68. Handling StaleElementReferenceException
Modern web applications can replace DOM elements during AJAX updates or framework re-rendering. A previously stored WebElement may then become stale.
try {
WebElement element = driver.findElement(By.id("message"));
System.out.println(element.isDisplayed());
} catch (StaleElementReferenceException e) {
System.out.println("Element became stale");
}
69. isDisplayed() and Dynamic Web Applications
Frameworks such as React, Angular, and other client-side applications can dynamically create, remove, hide, show, or replace elements.
For these applications, visibility should generally be synchronized with explicit waits rather than relying only on immediate calls.
WebDriverWait wait = new WebDriverWait(
driver,
Duration.ofSeconds(10)
);
WebElement element = wait.until(
ExpectedConditions.visibilityOfElementLocated(
By.id("dynamicElement")
)
);
70. isDisplayed() and AJAX
AJAX operations can update part of a web page without performing a complete page reload. An element may therefore become visible only after an asynchronous operation finishes.
driver.findElement(By.id("loadData")).click();
WebDriverWait wait = new WebDriverWait(
driver,
Duration.ofSeconds(10)
);
WebElement data = wait.until(
ExpectedConditions.visibilityOfElementLocated(
By.id("data")
)
);
Assert.assertTrue(data.isDisplayed());
71. isDisplayed() and Responsive Design
Responsive applications can show or hide different elements depending on viewport size. Therefore, an element displayed on desktop may not be displayed in the same way on a smaller viewport.
driver.manage().window().setSize(
new Dimension(1280, 720)
);
WebElement desktopMenu = driver.findElement(
By.id("desktopMenu")
);
System.out.println(
"Desktop Menu: " + desktopMenu.isDisplayed()
);
72. Mobile Responsive Visibility Check
driver.manage().window().setSize(
new Dimension(375, 812)
);
WebElement mobileMenu = driver.findElement(
By.id("mobileMenu")
);
System.out.println(
"Mobile Menu: " + mobileMenu.isDisplayed()
);
73. isDisplayed() Does Not Mean Enabled
A displayed button may still be disabled.
WebElement submit = driver.findElement(By.id("submit"));
boolean visible = submit.isDisplayed();
boolean enabled = submit.isEnabled();
System.out.println("Visible: " + visible);
System.out.println("Enabled: " + enabled);
For a control that must both be visible and enabled:
if (submit.isDisplayed() && submit.isEnabled()) {
System.out.println("Submit is visible and enabled");
}
74. isDisplayed() Does Not Mean Selected
A checkbox or radio button can be displayed without being selected.
WebElement checkbox = driver.findElement(By.id("terms"));
boolean visible = checkbox.isDisplayed();
boolean selected = checkbox.isSelected();
System.out.println("Visible: " + visible);
System.out.println("Selected: " + selected);
75. isDisplayed() Does Not Guarantee Click Success
An element being displayed does not guarantee that a click will always succeed. Other conditions can affect interaction, including overlays, animations, stale elements, application state, or click interception.
WebElement button = driver.findElement(By.id("submit"));
if (button.isDisplayed()) {
System.out.println("Button is displayed");
}
For click-oriented synchronization, an explicit wait such as elementToBeClickable() may be more appropriate.
76. isDisplayed() with elementToBeClickable()
WebDriverWait wait = new WebDriverWait(
driver,
Duration.ofSeconds(10)
);
WebElement button = wait.until(
ExpectedConditions.elementToBeClickable(
By.id("submit")
)
);
button.click();
This condition checks for the state needed for a typical click interaction, but it still does not guarantee that an application-specific overlay or event will never interfere with the click.
77. Checking Visibility Before Interaction
WebElement searchBox = driver.findElement(
By.id("search")
);
if (searchBox.isDisplayed()) {
searchBox.sendKeys("Selenium");
}
78. Checking Multiple Properties Together
WebElement submit = driver.findElement(By.id("submit"));
boolean displayed = submit.isDisplayed();
boolean enabled = submit.isEnabled();
if (displayed && enabled) {
System.out.println(
"Submit button is displayed and enabled"
);
}
79. Complete Visibility Validation Example
WebElement username = driver.findElement(By.id("username"));
WebElement password = driver.findElement(By.id("password"));
WebElement loginButton = driver.findElement(By.id("login"));
Assert.assertTrue(
username.isDisplayed(),
"Username field is not displayed"
);
Assert.assertTrue(
password.isDisplayed(),
"Password field is not displayed"
);
Assert.assertTrue(
loginButton.isDisplayed(),
"Login button is not displayed"
);
80. Practical Project Using isDisplayed()
A practical Selenium project can use isDisplayed() to validate the visibility of important components throughout an application.
Open Browser
↓
Open Application
↓
Verify Logo
↓
Verify Navigation Menu
↓
Verify Login Form
↓
Enter Credentials
↓
Click Login
↓
Verify Dashboard
↓
Verify Success Message
↓
Open Product Page
↓
Verify Product Cards
↓
Verify Add to Cart Button
↓
Open Cart
↓
Verify Checkout Form
↓
Complete Test
81. Practical Project Example
WebDriverWait wait = new WebDriverWait(
driver,
Duration.ofSeconds(10)
);
WebElement username = wait.until(
ExpectedConditions.visibilityOfElementLocated(
By.id("username")
)
);
WebElement password = wait.until(
ExpectedConditions.visibilityOfElementLocated(
By.id("password")
)
);
WebElement loginButton = wait.until(
ExpectedConditions.visibilityOfElementLocated(
By.id("login")
)
);
Assert.assertTrue(username.isDisplayed());
Assert.assertTrue(password.isDisplayed());
Assert.assertTrue(loginButton.isDisplayed());
username.sendKeys("testuser");
password.sendKeys("password");
loginButton.click();
WebElement dashboard = wait.until(
ExpectedConditions.visibilityOfElementLocated(
By.id("dashboard")
)
);
Assert.assertTrue(dashboard.isDisplayed());
82. Common Mistakes
- Assuming that findElement() means the element is visible.
- Using isDisplayed() as a synchronization mechanism.
- Using isDisplayed() when the real requirement is enabled state.
- Using isDisplayed() when the real requirement is selected state.
- Ignoring NoSuchElementException when an optional element may not exist.
- Keeping stale WebElements after DOM re-rendering.
- Assuming displayed means clickable in every situation.
- Ignoring overlays and animations.
- Ignoring responsive visibility changes.
- Using long Thread.sleep() calls instead of appropriate explicit waits.
83. Best Practices for isDisplayed()
- Use isDisplayed() when the test requirement is specifically about visibility.
- Use explicit waits for elements that appear dynamically.
- Use visibilityOfElementLocated() when the element must appear before continuing.
- Use invisibilityOfElementLocated() when waiting for an element to disappear.
- Use isEnabled() separately when enabled state must also be verified.
- Use isSelected() separately for checkbox, radio, and option selection state.
- Use stable locators such as ID, CSS Selector, or reliable XPath.
- Handle dynamic DOM updates carefully.
- Use Page Object Model methods for reusable visibility checks.
- Use meaningful assertion messages.
- Avoid unnecessary hard-coded waits.
- Do not use visibility checks as a substitute for proper element synchronization.
84. Advantages of isDisplayed()
| Advantage | Description |
| Simple | Easy to use and understand. |
| Boolean Result | Returns true or false for straightforward validation. |
| UI Validation | Useful for checking whether important elements are displayed. |
| Dynamic UI Testing | Useful for validating elements that appear or disappear. |
| Form Validation | Can verify that fields and messages are visible. |
| Regression Testing | Helps verify that expected UI components remain visible. |
| TestNG Integration | Can be combined easily with assertions. |
85. Limitations of isDisplayed()
- It is an immediate check and does not wait automatically.
- The element must generally be located before isDisplayed() can be called.
- It does not determine whether the element is enabled.
- It does not determine whether the element is selected.
- It does not guarantee that an element can be clicked successfully.
- Dynamic DOM changes can cause stale element problems.
- Responsive layouts can change element visibility.
- Overlays and animations can affect actual interaction even when an element is displayed.
86. Quick Reference
| Requirement | Recommended Method |
| Check displayed state | isDisplayed() |
| Check enabled state | isEnabled() |
| Check selected state | isSelected() |
| Wait for visibility | visibilityOfElementLocated() |
| Wait for an existing element to become visible | visibilityOf() |
| Wait for disappearance | invisibilityOfElementLocated() |
| Wait for clickability | elementToBeClickable() |
| Retrieve visible text | getText() |
| Retrieve attribute/property value | getAttribute() |
87. Interview Questions
Q1. What is isDisplayed() in Selenium?
isDisplayed() is a WebElement method used to check whether an element is displayed. It returns a boolean value.
Q2. What is the return type of isDisplayed()?
The return type is boolean.
Q3. What does true mean in isDisplayed()?
true means that Selenium considers the element to be displayed according to WebDriver's visibility rules.
Q4. What does false mean in isDisplayed()?
false means that the element is not displayed according to WebDriver's visibility rules.
Q5. Does isDisplayed() wait for an element?
No. isDisplayed() performs an immediate check. For dynamic elements, use an explicit wait such as visibilityOfElementLocated().
Q6. What is the difference between isDisplayed() and isEnabled()?
isDisplayed() checks displayed state, while isEnabled() checks whether an element is enabled for interaction.
Q7. What is the difference between isDisplayed() and isSelected()?
isDisplayed() checks visibility, while isSelected() checks selected state, especially for checkboxes, radio buttons, and options.
Q8. Can isDisplayed() be used with TestNG?
Yes. For example:
Assert.assertTrue(element.isDisplayed());
Q9. Can isDisplayed() verify a hidden element?
Yes. It can return false when the located element is not displayed.
Q10. Does findElement() guarantee that an element is displayed?
No. findElement() locates an element in the DOM. The element can still be non-displayed.
Q11. What should be used for waiting until an element becomes visible?
WebDriverWait with ExpectedConditions.visibilityOfElementLocated() is commonly used.
Q12. How do you check whether an element is displayed?
boolean visible = element.isDisplayed();
Q13. How do you assert that an element is displayed?
Assert.assertTrue(element.isDisplayed());
Q14. Can a displayed element be disabled?
Yes. Displayed state and enabled state are separate properties.
Q15. Does isDisplayed() guarantee that an element can be clicked?
No. An element can be displayed but still be affected by overlays, animations, stale references, application state, or other interaction issues.
Q16. What exception can occur when the element cannot be found?
findElement() can throw NoSuchElementException when the requested element cannot be located.
Q17. What exception can occur when the DOM element is replaced?
StaleElementReferenceException can occur when a previously located WebElement is no longer attached to the current DOM.
Q18. How do you wait for an element to disappear?
wait.until(
ExpectedConditions.invisibilityOfElementLocated(
By.id("loader")
)
);
88. Best Practices Checklist
- Use isDisplayed() specifically for visibility validation.
- Remember that isDisplayed() is an immediate check.
- Use WebDriverWait for dynamic visibility requirements.
- Use visibilityOfElementLocated() for elements that need to appear.
- Use invisibilityOfElementLocated() for elements that need to disappear.
- Use isEnabled() for enabled-state validation.
- Use isSelected() for selection-state validation.
- Use stable locators.
- Handle NoSuchElementException when optional elements may be absent.
- Handle StaleElementReferenceException on frequently changing pages.
- Do not assume displayed means clickable.
- Consider responsive layouts and dynamic UI behavior.
- Use Page Object Model for reusable visibility methods.
- Use TestNG assertions for clear automated validation.
89. Conclusion
isDisplayed() is an important Selenium WebDriver method used to determine whether a web element is currently displayed. It returns a boolean value and can be used for validating buttons, forms, fields, messages, menus, images, popups, product cards, and other UI elements.
The method should be clearly distinguished from isEnabled() and isSelected(). isDisplayed() checks displayed state, isEnabled() checks enabled state, and isSelected() checks selection state.
For static pages, isDisplayed() can provide a simple immediate visibility check. For dynamic applications, explicit waits such as visibilityOfElementLocated() are generally more appropriate because they synchronize the test with the expected UI state.
Reliable Selenium automation should combine visibility checks with stable locators, explicit waits, appropriate assertions, proper exception handling, and Page Object Model practices.
JustAcademy's Selenium Training covers Selenium WebDriver, WebElements, locators, TestNG, automation frameworks, dynamic elements, practical testing, and real-time automation projects. Visit the JustAcademy Selenium Training Course to learn more. For a course demonstration, use the JustAcademy Course Demo Registration.