App & Browser Testing Made Easy

Give your users a seamless experience by testing on 3000+ real devices and browsers. Don't compromise with emulators and simulators

Home Guide How does Selenium isDisplayed() method work?

How does Selenium isDisplayed() method work?

By Shreya Bose, Community Contributor -

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.

An explainer for iselementpresent Selenium

  • iselementpresent is not a built-in function in Selenium. Still, it is a standard method used in Selenium test automation frameworks to check if an element is present on a web page.
  • In Selenium, to interact with an element on a web page, you must first locate it using a locator strategy such as ID, class name, name, XPath, or CSS selector.
  • However, sometimes the element may not be present on the page at the time of execution, and your test may fail if you try to interact with it.
  • In such cases, you can use the iselementpresent method to check if the element exists on the page before trying to interact with it.
  • The iselementpresent method returns a boolean value indicating whether the element is present on the page. If the element is present, the method returns True; otherwise, it returns False.
  • You can use this method in your test automation code to conditionally execute certain actions based on whether an element is present or not.

Now, let’s explore three methods to check the visibility of web elements. These elements include drop boxes, buttons, checkboxes, and labels. 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();

Also read: Effective ways to use XPath in Selenium


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.

Tags
Automation Testing Selenium Selenium Webdriver

Featured Articles

findElement and findElements in Selenium

Locators in Selenium: A Detailed Guide

Curated for all your Testing Needs

Actionable Insights, Tips, & Tutorials delivered in your Inbox
By subscribing , you agree to our Privacy Policy.
thank you illustration

Thank you for Subscribing!

Expect a curated list of guides shortly.