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

Software TestingAutomation TestingSelenium Web Driver

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.


import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import java.util.concurrent.TimeUnit;
public class ElementStatus{
   public static void main(String[] args) {
      WebDriver driver = new ChromeDriver();
      String url =
      // to verify if a static dropdown is selected with option isSelected()
      boolean drpdwnStatus = driver.findElement(By.xpath("//select[@name=’selType’]"))
      // to verify if an element is present on page with isDisplayed()
      boolean editStatus = driver.findElement(By.xpath("//input[@id=’txtSearchText’]"))
      // to verify if a button is enabled with isEnabled()
      boolean butnStatus = driver.findElement(By.xpath("//input[@id=’searchSubmit’]"))
      System.out.println("The button status is " + butnStatus);
      ("The dropdown selected status is" + drpdwnStatus);
      System.out.println("The edit box display status is " + editStatus);
Published on 11-Jun-2020 15:37:49