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:

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import java.util.concurrent.TimeUnit;

public class VerifyConditionsDemo {

    public static void main(String[] args) {

        System.setProperty("webdriver.gecko.driver", "C:\\Users\\Vibha\\Software\\geckodriver-v0.31.0-win64\\geckodriver.exe");

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

        // Maximize the browser
        driver.manage().window().maximize();
        driver.manage().timeouts().implicitlyWait(10, 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:

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import java.util.List;
import java.util.concurrent.TimeUnit;

public class isSelectedDemo {

    public static void main(String[] args) {
        System.setProperty("webdriver.gecko.driver", "C:\\Users\\Vibha\\Software\\geckodriver-v0.31.0-win64\\geckodriver.exe");

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

        // 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!!

Advertisement

How to automate Radio Button in Selenium WebDriver

HOME

 

In this tutorial, will find out how the Radio Buttons can automated in Selenium WebDriver. Radio Button is also a Web Element like Checkbox. 
Below image shows the Radio Buttons before they selected

Here, In below image Male option is selected and clicked on Get Checked value, then a message will be displayed.

Here,
Check if Option 1 is already selected or not
If Option 1 is already selected, then select Option 2, else select Option 1.

Let’s go through the scenario below:-

1) Launch Chrome Browser
2) Maximize the current window
3) Implicitly wait for 30 sec
4) Open browser – https://www.seleniumeasy.com/test/basic-radiobutton-demo.html.
5) Find locator of all Radio Buttons
6) Find no of Radio Buttons available and print their values
7) Verify that first Radio button is selected or not and print
8) Find the value of Get Checked Value button and print the value
9) Identify if Radio Button 1 selected or not. If Button 1 already selected, then select Button 2
10) Click on “Get Checked Value” button
11) Find the value of “Get Checked Value” button after selecting the option and print the value
12) Close the browser

import java.util.List;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
 
public class RadioButtonDemo {
            public static void main(String[] args) throws InterruptedException {
 
                        System.setProperty("webdriver.chrome.driver", "src\\test\\resources\\webdrivers\\window\\chromedriver.exe");
 
                        // Initiate Chrome browser
                        WebDriver driver = new ChromeDriver();
 
                        // Maximize the browser
                        driver.manage().window().maximize();
 
                        // Put an Implicit wait and launch URL
                        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
                        driver.get("https://www.seleniumeasy.com/test/basic-radiobutton-demo.html");
 
                        // Find locator of all Radio Buttons
                        List Radio_Options = driver.findElements(By.xpath("//*[@name='optradio']"));
 
                        // Find no of Radio Buttons available and print their values
                        int radioSize = Radio_Options.size();
                        System.out.println("No Of Radio Button Options :" + radioSize);
 
                        for (int i= 0; i< radioSize; i++) {
                                    System.out.println("Name of Radio Button :"+ Radio_Options.get(i).getAttribute("value"));
                        }
 
                        // 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 selected
                        radio_value = Radio_Options.get(0).isSelected();
                        System.out.println("First Radio Option is Checked :" + radio_value);
 
                        // Find the value of "Get Checked Value" button and print the value
                        String preButtonSelected = driver.findElement(By.xpath("//*[@id='easycont']/div/div[2]/div[1]/div[2]/p[3]"))
                                                .getText();
 
                        if (preButtonSelected.isEmpty() == true) {
                                    System.out.println("Get Checked Value before selection is Empty");
                        } else {
                                    System.out.println("Get Checked Value before selection is :" + preButtonSelected);
                        }
                        Thread.sleep(1000);
 
                        // Identify if Radio Button 1 is selected or not. If Button 1 is already
                        // selected, then select Button 2
                        if (radio_value == true) {
                                    Radio_Options.get(1).click();
                                    System.out.println("Button Selected is :"+ Radio_Options.get(1).getAttribute("value"));
                        } else {
                                    Radio_Options.get(0).click();
                                    System.out.println("Button Selected is :"+ Radio_Options.get(0).getAttribute("value"));
                        }
 
                        // Click on "Get Checked Value" button
                        driver.findElement(By.id("buttoncheck")).click();
 
                        // Find the value of "Get Checked Value" button after selecting 
                        // the option and print the value
                        String postButtonSelected = driver.findElement(By.xpath("//*[@id='easycont']/div/div[2]/div[1]/div[2]/p[3]"))
                                                .getText();
                        System.out.println("Get Checked Value is :"+ postButtonSelected);
                        Thread.sleep(1000);
 
                        // Close the browser
                        driver.close();
            }
}

Output
No Of Radio Button Options :2
Name of Radio Button :Male
Name of Radio Button :Female
First Radio Option is Checked :false
Get Checked Value before selection is Empty
Button Selected is :Male
Get Checked Value is :Radio button 'Male' is checked