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

SeleniumAutomation TestingTesting Tools

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

raja
Published on 18-Sep-2020 11:14:07
Advertisements