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