isDisplayed, isSelected, isEnabled in Selenium

HOME

This tutorial describes how to check the state of a WebElement.

In this tutorial, we will learn the isDisplayed, isEnabled, isSelected method in Selenium, and how to check the state of a WebElement. There are many methods that are used to determine the visibility scope for the web elements – isSelected(), isEnabled(), and isDispalyed().

Many a time, a test fails when we click on an element or enter text in a field. This is because the element is displayed or exists in DOM, but it does not exist on the web page.

WebDriver facilitates the user with the following methods to check the visibility of the web elements. These web elements can be buttons, drop boxes, checkboxes, radio buttons, labels, etc.

  • isDisplayed()
  • isSelected()
  • isEnabled()

1) Boolean isSelected(): This method determines if an element is selected or not. It returns true if the element is selected and false if it is not. It is widely used on checkboxes, radio buttons, and options in a select.

2) Boolean isDisplayed(): This method determines if an element is displayed or not. It returns true if the element is displayed and false if it is not. The advantage of this method is that it avoids parsing an element’s style attribute.

3) Boolean isEnabled(): This method determines if an element is enabled or not. It returns true if the element is enabled (All elements apart from disabled input elements) and false if otherwise.

Steps to follow to understand when an element isEnabled and isDisplayed.

  1. Launch the web browser and open the application under test – https://duckduckgo.com/
  2. Verify the web page title
  3. Verify if the “Search Box” is displayed
  4. Verify that the “Search Box” is enabled
  5. If Search Box is enabled, then search for text – Selenium
  6. Close the browser

The complete program is shown below:

package org.example;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;

import java.util.concurrent.TimeUnit;

public class VerifyConditionsDemo {

    public static void main(String[] args) {

        // Initiate Firefox browser
        FirefoxOptions options = new FirefoxOptions();
        WebDriver driver = new FirefoxDriver(options);

        // Maximize the browser
        driver.manage().window().maximize();
        driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);

        // launch the firefox browser and open the application url
        driver.get("https://duckduckgo.com/");

        // compare the expected title of the page with the actual title of the page and
        String expectedTitle = "DuckDuckGo — Privacy, simplified.";
        String actualTitle = driver.getTitle();
        if (expectedTitle.equals(actualTitle)) {
            System.out.println("Verification Pass- The correct title is displayed on the web page.");
        } else {
            System.out.println("Verification Failed - An incorrect title is displayed on the web page.");
        }

        // Verify that the “Search" Box is displayed
        WebElement searchBox = driver.findElement(By.className("searchbox_input__bEGm3"));
        if (searchBox.isDisplayed()) {
            System.out.println("Search Box is visible. Return: " + searchBox.isDisplayed());
        } else {
            System.out.println("Search Box is not visible. Return: " + searchBox.isDisplayed());
        }

        // Verify that the “Search” Box is enabled
        if (searchBox.isEnabled()) {
            System.out.println("Search Box is enabled. Return: " + searchBox.isEnabled());
            searchBox.sendKeys("Selenium");
        } else {
            System.out.println("Search Box is not enabled. Return: " + searchBox.isEnabled());
        }

        System.out.println("Successful Execution of Test.");

        // close the web browser
        driver.close();

    }
}

The output of the above program is

isSelected

Steps to follow to understand when an element is Selected or not

  1. Launch the web browser and open the application under test – https://demoqa.com/radio-button
  2. Print all the options for the Radio Button
  3. Verify if the first Radio Button (Yes) is selected or not
  4. If the first radio button is not selected, then select Radio Button 1, else select button 2.
  5. Print the value of the selected Radio Button
  6. Close the browser

Below is the image of the options for the Radio Button.

The complete program is shown below:

package org.example;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;

import java.util.List;
import java.util.concurrent.TimeUnit;

public class isSelectedDemo {

        public static void main(String[] args) {


            // Initiate Firefox browser
            FirefoxOptions firefoxOptions = new FirefoxOptions();
            WebDriver driver = new FirefoxDriver(firefoxOptions);

            // Maximize the browser
            driver.manage().window().maximize();
            driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

            driver.get("https://demoqa.com/radio-button");

            List<WebElement> Radio_Options = driver.findElements(By.cssSelector(".custom-radio"));
            for(WebElement options: Radio_Options)
                System.out.println("Options :"+ options.getText());

            // Create a boolean variable which will hold the value (True/False)
            boolean radio_value = false;

            // This statement will return True, in case of first Radio button is already selected
            radio_value = Radio_Options.get(0).isSelected();
            System.out.println("First Option is already selected :"+radio_value);

            // If button 1 is not selected, then select otherwise select button 2
            if (radio_value == false) {
                Radio_Options.get(0).click();
                System.out.println("Button Selected is :" + Radio_Options.get(0).getText());
            } else {
                Radio_Options.get(1).click();
                System.out.println("Button Selected is :" + Radio_Options.get(1).getText());
            }

            // close the web browser
            driver.close();
        }
    }

The output of the above program is

That’s it! Congratulations on making it through this tutorial and hope you found it useful! Happy Learning!! Cheers!!

Leave a comment