Home Guide How does Selenium isDisplayed() method work?

How does Selenium isDisplayed() method work?

Shreya Bose, Technical Content Writer at BrowserStack -

This article explains how the isDisplayed() method works in Selenium, and why it is used. It will also explore two other looping and conditional commands – isSelected() and isEnabled().

These methods are used when a tester wants to ascertain the visibility scope for various web elements.

Before beginning, it must be mentioned that WebDriver has a W3C specification that lays out information on different visibility preferences. This data is based on the types of web elements on which commands are to be executed.

Now, let’s explore three methods to check the visibility of web elements. These elements can include drop boxes, buttons, checkboxes, labels and more. These Selenium WebDriver methods are best explained with examples of code.

In the code snippet below, the following actions are automated:

  1. Open Chrome browser
  2. Go to Gmail.com
  3. Type in email id
  4. Click the next
import org.openqa.selenium.By;
import org.openqa.selenium.Point;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
public class TestTestTest {

public static void main (String [] args) throws InterruptedException {
//Instantiation of driver object. To launch Firefox browser 
System.setProperty("webdriver.chrome.driver", "D:\\Selenium Environment\\Drivers\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
//To open gmail
driver.get("http://www.gmail.com");
//isDisplayed() method returns boolean value either True or False
Boolean Display = driver.findElement(By.xpath("//*[@id='next']")).isDisplayed();
//To print the value
System.out.println("Element displayed is :"+Display);
//isEnabled() method returns boolean value either True or False 
Boolean Enable = driver.findElement(By.xpath("//*[@id='next']")).isEnabled();
System.out.println("Element enabled is :"+Enable);
//Passing value as "softwaretestingmaterial" in the email field
driver.findElement(By.xpath("//*[@id='Email']")).sendKeys("<put_an_email_id>");
//to click on next button
driver.findElement(By.xpath("//*[@id='next']")).click();
//isSelected() method returns boolean value either True or False
Boolean Select = driver.findElement(By.xpath("//*[@id='PersistentCookie']")).isSelected();
System.out.println("Element selected is :"+Select);
} 
}

Try Selenium Testing on Real Device Cloud for Free

Let’s understand each of the methods used –

1. isDisplayed()

The isDisplayed method in Selenium verifies if a certain element is present and displayed. If the element is displayed, then the value returned is true. If not, then the value returned is false.

The code below verifies if an element with the id attribute value next is displayed.

Syntax:

boolean eleSelected= driver.findElement(By.xpath("xpath")).isDisplayed();

2. isSelected()

This method is often used on radio buttons, checkboxes or options in a menu. It is used to determine is an element is selected. If the specified element is selected, the value returned is true. If not, the value returned is false.

The code below verifies if an element with the id attribute value PersistentCookie is displayed.

Syntax:

boolean elePresent = driver.findElement(By.xpath("xpath")).isSelected();

3. isEnabled()

This method verifies if an element is enabled. If the element is enabled, it returns a true value. If not, it returns a false value.

The code below verifies if an element with the id attribute value next is enabled.

Syntax:

boolean eleEnabled= driver.findElement(By.xpath("xpath")).isEnabled();

Since automated Selenium testing is essential for testing of websites and web-apps in an agile manner, mastering the use of these three methods is mandatory for QAs or anyone who uses Selenium for automation purposes. It is important to learn and use the basic commands in Selenium, so that QAs can automate tests quickly and establish an effective software testing pipeline.

BrowserStack Logo Run Selenium Tests on 2000+ Browsers & Devices Get Started Free