How does Selenium WebDriver's isDisplayed() method work?


We can work with isDisplayed() method in Selenium webdriver. This method checks if a webelement is visible on the page. If it is visible, then the method returns a true value, else it returns false.

First of all, we have to identify the element with any of the locators like id, class, name, xpath or css and then apply isDisplayed() method on it.

Syntax

boolean s= driver.findElement(By.id("txt-bx")).isDisplayed();

Let us check if the element About Careers at Tutorials Point is displayed on the page. Since it is available it shall return a true value.

Example

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.concurrent.TimeUnit;

public class VisibleElement{
   public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver","C:\Users\ghs6kor\Desktop\Java\chromedriver.exe");
      WebDriver driver = new ChromeDriver();
      driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
      driver.get("https://www.tutorialspoint.com/about/about_careers.htm");
      // identify element
      WebElement p=driver.findElement(By.xpath("//h1"));
      //isDisplayed() to check if element visible
      boolean s= p.isDisplayed();
      System.out.println("The return value: " + s);
      driver.close();
   }
}

Output

Updated on: 18-Sep-2020

965 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements