isSelected() method in Selenium Java

isSelected() method is used to determine whether the element is selected or not. This post will discuss the isSelected() method in detail.

  • Method declaration – boolean isSelected()
  • What does it do? – It determines whether an element is selected or not. This only applies to the input elements such as checkboxes, options in a select, and radio buttons.
  • What does it return? It will return true if the element is selected. Otherwise, it will return false.
A Note:

IsSelected will only work with below elements –

  • Input element with type attribute = checkbox
  • Input element with type attribute = radio
  • and if it’s an option element

We will take a pre-selected radio button element and then use the isSelected() method on that element to see if the radio button is selected or not.

We will use our selenium playground website to show you a working example. The pre-selected radio button is present on this page – https://testkru.com/Elements/RadioButtons.

Pre-selected radio button
  • We will use the id attribute to find the radio button element.
  • The id of our element is firstSelect5.
  • We can use findElement(By.id(“firstSelect5”)) to find the element and we will use the isSelected() method to find if the radio button is pre-selected or not.
public class CodekruTest {
	 
    @Test
    public void test() {
 
        // pass the path of the chromedriver location in the second argument
        System.setProperty("webdriver.chrome.driver", "/usr/local/bin/chromedriver");
        WebDriver driver = new ChromeDriver();
 
        // opening the url
        driver.get("https://testkru.com/Elements/RadioButtons");
        
        WebElement element = driver.findElement(By.id("firstSelect5"));

        System.out.println("Is element selected: " + element.isSelected());
    }
}

Output –

Is element selected: true
Let’s use the function where an element is not pre-selected
Simple radio button
public class CodekruTest {
	 
    @Test
    public void test() {
 
        // pass the path of the chromedriver location in the second argument
        System.setProperty("webdriver.chrome.driver", "/usr/local/bin/chromedriver");
        WebDriver driver = new ChromeDriver();
 
        // opening the url
        driver.get("https://testkru.com/Elements/RadioButtons");
        
        WebElement element = driver.findElement(By.id("firstSelect1"));

        System.out.println("Is element selected: " + element.isSelected());
    }
}

Output –

Is element selected: false
Using the isSelected() method after an element changes its state
  • We will pick an element that wasn’t selected at first
  • Then, we select that element
  • And afterward, we use the isSelected() method to see if it returns the element’s current state, which is selected.
Before selecting the button
After selecting the button
public class CodekruTest {
	 
    @Test
    public void test() {
 
        // pass the path of the chromedriver location in the second argument
        System.setProperty("webdriver.chrome.driver", "/usr/local/bin/chromedriver");
        WebDriver driver = new ChromeDriver();
 
        // opening the url
        driver.get("https://testkru.com/Elements/RadioButtons");
        
        WebElement element = driver.findElement(By.id("firstSelect1"));
        
        System.out.println("Is element selected before selecting: " + element.isSelected());
        
        // selecting the radio button
        element.click();
        
        System.out.println("Is element selected after selecting: " + element.isSelected());
    }
}

Output –

Is element selected before selecting: false
Is element selected after selecting: true

We can see that the isSelected() method represented the element’s current state.

What if we use the isSelected() method on a null element?

It will throw a NullPointerException, as illustrated by the below example.

public class CodekruTest {

	@Test
	public void test() {

		// pass the path of the chromedriver location in the second argument
		System.setProperty("webdriver.chrome.driver", "/usr/local/bin/chromedriver");
		WebDriver driver = new ChromeDriver();

		// opening the url
		driver.get("https://testkru.com/Elements/RadioButtons");

		WebElement element = null;

		System.out.println("Is element selected after selecting: " + element.isSelected());
	}
}

Output –

java.lang.NullPointerException: Cannot invoke "org.openqa.selenium.WebElement.isSelected()" because "element" is null

This is it. Please visit this link to learn more about WebElement and its methods.

We hope that you have liked the article. If you have any doubts or concerns, please feel free to write us in the comments or mail us at [email protected]

Liked the article? Share this on

Leave a Comment

Your email address will not be published.