"Seleniums.com is where I keep my notes organized."

isDisplayed()

Estimated reading: 5 minutes 21 views

Overview

The isDisplayed() method in Selenium WebDriver is used to determine whether a web element is visible on the web page. It returns a boolean value: true if the element is visible to the user, and false if the element is not visible (e.g., it is hidden, outside the viewport, or has a display: none style).

This method is critical for checking the visibility of elements during automated testing, as many tests depend on whether an element is actually visible and can interact with the user interface.

Syntax

				
					boolean isDisplayed = element.isDisplayed();
				
			
  • element: The WebElement whose visibility is being checked.
  • isDisplayed(): Returns a boolean value: true if the element is visible, false otherwise.

Usage

The isDisplayed() method is commonly used for:

  1. Checking Element Visibility: This is often used in cases where the visibility of an element needs to be verified before interacting with it, such as checking if a button or link is visible before clicking it.

				
					WebElement loginButton = driver.findElement(By.id("loginBtn"));
if (loginButton.isDisplayed()) {
    loginButton.click();
} else {
    System.out.println("Login button is not visible.");
}
				
			

2. Verifying UI State: After performing some actions (such as submitting a form or clicking a button), you might want to confirm whether an element becomes visible or invisible as part of the application’s flow.

				
					WebElement successMessage = driver.findElement(By.id("success"));
if (successMessage.isDisplayed()) {
    System.out.println("Success message is displayed!");
}
				
			

3. Element Visibility Check for Assertions: In test automation, isDisplayed() can be used for assertions to ensure that certain elements appear only when expected, such as ensuring an error message is displayed after submitting an incomplete form.

				
					WebElement errorMessage = driver.findElement(By.id("error"));
Assert.assertTrue(errorMessage.isDisplayed(), "Error message should be visible.");
				
			

4. Waiting for an Element to Become Visible: You can also combine isDisplayed() with explicit waits to wait for elements to appear on the page before interacting with them.

				
					WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("someElement")));
if (element.isDisplayed()) {
    System.out.println("Element is now visible.");
}
				
			

Example

				
					import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class IsDisplayedExample {
    public static void main(String[] args) {
        // Set path to the ChromeDriver executable
        System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");

        // Initialize WebDriver (ChromeDriver in this case)
        WebDriver driver = new ChromeDriver();

        // Navigate to a page
        driver.get("https://www.example.com");

        // Find an element and check if it is displayed
        WebElement loginButton = driver.findElement(By.id("loginBtn"));
        if (loginButton.isDisplayed()) {
            loginButton.click();
            System.out.println("Login button clicked.");
        } else {
            System.out.println("Login button is not visible.");
        }

        // Close the browser
        driver.quit();
    }
}
				
			

Importance

The isDisplayed() method is important because:

  • Visibility Verification: It ensures that an element is not only present in the DOM but also visible on the screen. This is crucial for interacting with elements in tests, as hidden elements (via display: none, visibility: hidden, etc.) cannot be interacted with.
  • User Interface Testing: It helps ensure that elements (e.g., buttons, links, popups) appear and disappear as expected based on the application’s functionality.
  • Conditional Actions: Using isDisplayed(), you can conditionally perform actions like clicking, typing, or asserting that an element is visible or hidden based on the application state.
  • Improving Test Stability: By checking visibility before interacting with an element, isDisplayed() helps avoid exceptions caused by trying to interact with invisible or non-existent elements.

Limitations

While isDisplayed() is very useful, there are some limitations:

  • Not Effective for Elements Outside the Viewport: The method returns true if an element is within the DOM, even if it is outside the visible viewport (e.g., off-screen or hidden by a scroll). For such cases, you may need to scroll the element into view using JavaScript.
  • Hidden but Present Elements: If an element is hidden (using display: none, visibility: hidden, or similar CSS properties), isDisplayed() will return **false**, but the element may still be present in the DOM. This could lead to misinterpretation if not combined with other checks for element presence.
  • Timing Issues with Dynamic Elements: For elements that are dynamically loaded, there could be cases where the element is not yet visible due to delays in rendering. In such cases, using WebDriverWait or ExpectedConditions.visibilityOf() should be preferred.

Conclusion

The isDisplayed() method is essential for verifying the visibility of elements in Selenium WebDriver. It helps testers confirm whether an element is visible on the page, enabling more precise interaction with elements during tests. isDisplayed() is particularly useful for UI verification, conditional actions, and dynamic content validation. While straightforward, it’s important to consider the element’s visibility in different contexts to avoid false results.

Key Features

  • Visibility Check: Verifies if an element is visible on the screen.
  • Returns Boolean: Returns true if the element is visible, false otherwise.
  • UI Consistency: Ensures elements are present and visible when they should be, particularly in user interface testing.
  • Conditional Interaction: Used to conditionally interact with elements, ensuring actions like clicks or text input are only performed on visible elements.
  • Integration with Waits: Works well with explicit waits to wait for elements to become visible before interacting with them.

Leave a Comment

Share this Doc

isDisplayed()

Or copy link

CONTENTS