How to verify if an element is displayed on screen in Selenium?


We can verify the visibility of web elements like edit box, checkbox, radio buttons and so on with the help of method listed bels −

  • isDisplayed()

    This method checks if a webelement is present on the screen.

    Syntax −

    Boolean result = driver.findElement(By.xpath("//span[text()=’Coding Ground’]")).isDispayed();
  • isSelected()

    This method checks the status of the radio button, check box and options in the static dropdown.

    Syntax −

    Boolean btnresult = driver.findElement(By.xpath("//xpath[contains(@class,’gsc-search-button’)]")).isSelected();
  • isEnabled()

    Syntax −

    Boolean btnresult = driver.findElement(By.xpath("//xpath[contains(@class,’gsc-search-button’)]")).isEnabled();

This method if an element is enabled or not.

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 ElementStatus{
   public static void main(String[] args) {
      System.setProperty("webdriver.chrome.driver",
      "C:\Users\ghs6kor\Desktop\Java\chromedriver.exe");
      WebDriver driver = new ChromeDriver();
      String url =
      "https://www.tutorialspoint.com/tutor_connect/index.php";
      // to verify if a static dropdown is selected with option isSelected()
      boolean drpdwnStatus = driver.findElement(By.xpath("//select[@name=’selType’]"))
      .isSelected();
      // to verify if an element is present on page with isDisplayed()
      boolean editStatus = driver.findElement(By.xpath("//input[@id=’txtSearchText’]"))
      .isDisplayed();
      // to verify if a button is enabled with isEnabled()
      boolean butnStatus = driver.findElement(By.xpath("//input[@id=’searchSubmit’]"))
      .isEnabled();
      System.out.println("The button status is " + butnStatus);
      System.out.println
      ("The dropdown selected status is" + drpdwnStatus);
      System.out.println("The edit box display status is " + editStatus);
      driver.close();
   }
}

Updated on: 11-Jun-2020

8K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements