How to Select CheckBox and Radio Button in Selenium WebDriver

Select CheckBox & Radio Button in Selenium WebDriver: In this automation world, its quite common that when you are trying to automate Web Form that time, you can see that a web form contains Checkboxes and Radio buttons. These elements enhance the functionality of the web page by providing facilities like selecting single and multiple values.

Until this tutorial, we have seen how we can locate the various type of elements with the help of different locator strategies. Still, in this tutorial, we are going to learn how we can perform operations on Checkboxes and radio buttons. There are few new operations we need to verify on these two elements like is the checkbox is already checked, or the radio button is already selected and verify their default states.

How to Select CheckBox & Radio Button

We can try to learn the various possible ways to select the elements with real-time examples and also verify the current status. So that you can understand easily.

For Selecting any element (Check Box / Radio button) you need to click on the element, so the command for this operation it looks like below:

// Java code example to select checkbox/radio button.
WebElement target = driver.findElement(By.id("checkbox1"));
target.click();

When ID Of Each radio Button Is Unique

package com.selenium.practice.checkboxRadiobutton;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class RadiobuttonWithUniqueID 
{
   public static void main(String[] args) throws InterruptedException 
   {
      WebDriver driver;
      System.setProperty("webdriver.chrome.driver","Path Of Browser Driver");
      
      //When The Checkbox/Radio button have an Unique ID
      driver=new ChromeDriver();
      driver.manage().window().maximize();
      driver.get("https://softwaretestingo.blogspot.com/2020/08/checkbox-radio-button.html");
      driver.manage().timeouts().implicitlyWait(15000, TimeUnit.SECONDS);
      
      driver.findElement(By.id("python")).click();
      System.out.println("Python Selected");
      Thread.sleep(5000);
      driver.close();
   }
}

When Multiple Elements Have the Same ID

package com.selenium.practice.checkboxRadiobutton;

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 RadiobuttonWithoutUniqueID 
{
   public static void main(String[] args) throws InterruptedException 
   {
      WebDriver driver;
      System.setProperty("webdriver.chrome.driver","Path Of Browser Driver");

      //When The Checkbox/Radio button have an Unique ID
      driver=new ChromeDriver();
      driver.manage().window().maximize();
      driver.get("https://softwaretestingo.blogspot.com/2020/08/checkbox-radio-button.html");
      driver.manage().timeouts().implicitlyWait(15000, TimeUnit.SECONDS);

      List<WebElement> checkBoxes= driver.findElements(By.xpath("//input[@id='checkbox']"));
      System.out.println("Select Ruby Radio Button: "+checkBoxes.size());
      for(WebElement options : checkBoxes)
      {
         String linkName = options.getAttribute("value");
         if (linkName.equals("Selenium")) 
         {
            options.click();
            break; // Exit from the loop, this is important
         }
      }
      Thread.sleep(5000);
      driver.close();
   }
}

How to Deal with CheckBox

package com.selenium.practice.checkboxRadiobutton;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class CheckBoxEx 
{
   public static void main(String[] args) throws InterruptedException 
   {

      WebDriver driver;
      System.setProperty("webdriver.chrome.driver","Path Of Browser Driver");

      //When The Checkboxes have an Unique ID
      driver=new ChromeDriver();
      driver.manage().window().maximize();
      driver.get("https://softwaretestingo.blogspot.com/2020/08/checkbox-radio-button.html");
      driver.manage().timeouts().implicitlyWait(15000, TimeUnit.SECONDS);
      
      //Select Singing CheckBox
      driver.findElement(By.id("sing")).click();
      Thread.sleep(3000);
      System.out.println("Singing Check Box Selected");
      driver.close();
   }
}

How to Check the state Of Radio Button Or Checkboxes?

Sometimes before doing any action, we need to check the state of the Radio button or checkbox, and according to their state, we decide what operation we have to perform. So to verify the state of the element, we can use the IsSelected() method. When we execute the IsSelected() method, it will return a boolean value like true or false. Based on that, we can get to know the state of the element like if it is true, then that means the element is selected, and if it is false, then that means that specific element is not selected.

It’s better to verify a few things before performing any operation on the elements, like:

  • If Radio button or Checkbox is displayed on the webpage
  • If Radio button or Checkbox is enabled on the webpage
  • Check the default selection of the Radio button or Checkbox

We can verify these things as mentioned earlier with the help of the below-predefined methods in selenium webdriver:

isDisplayed(): This method returns a boolean value. if it returns true, Which means the web element is displaying on the web page. If the element is not displaying on the web page then it will return a Boolean value false.

WebElement eleBtn = driver.findElement (By.id("testing"));
eleBtn.isDisplayed();
package com.selenium.practice.checkboxRadiobutton;

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 IsDisplayedEx 
{
   public static void main(String[] args) throws InterruptedException 
   {

      WebDriver driver;
      System.setProperty("webdriver.chrome.driver","Path Of Browser Driver");

      //When The Checkboxes have an Unique ID
      driver=new ChromeDriver();
      driver.manage().window().maximize();
      driver.get("https://softwaretestingo.blogspot.com/2020/08/checkbox-radio-button.html");
      driver.manage().timeouts().implicitlyWait(15000, TimeUnit.SECONDS);
      
      //Locate The Element
      WebElement singing=driver.findElement(By.id("sing"));
      //If Element is Displaying Then Return True else It Will Return False
      Boolean CheckboxValue=singing.isDisplayed();
      if(CheckboxValue)
      {
         System.out.println("Singing Is Displpaying On WebPage");
      }
      else
         System.out.println("Singing Is Not Displaying In the Webpage");
      driver.close();
   }
}

isEnabled(): This method return an boolean value. This method is used to verify that a web element is enabled or disabled within a web page.

WebElement eleBtn = driver.findElement (By.id("testing"));
eleBtn.isEnabled;
package com.selenium.practice.checkboxRadiobutton;

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 isEnabledEx 
{
   public static void main(String[] args) throws InterruptedException 
   {

      WebDriver driver;
      System.setProperty("webdriver.chrome.driver","Path Of Browser Driver");

      //When The Checkboxes have an Unique ID
      driver=new ChromeDriver();
      driver.manage().window().maximize();
      driver.get("https://softwaretestingo.blogspot.com/2020/08/checkbox-radio-button.html");
      driver.manage().timeouts().implicitlyWait(15000, TimeUnit.SECONDS);
      
      //Locate The Element
      WebElement singing=driver.findElement(By.id("sing"));
      //If Element is Enabled Then Return True else It Will Return False
      Boolean CheckboxValue=singing.isEnabled();
      if(CheckboxValue)
      {
         System.out.println("Singing Is Enabled");
      }
      else
         System.out.println("Singing Is Disabled");
      driver.close();
   }
}

isSelected(): IsSelected() method is used to verify the other element is selected or not. If the element is selected then this method will be returned boolean value true else you will be getting a false value.

WebElement eleBtn = driver.findElement (By.id("testing"));
eleBtn.isSelected();
package com.selenium.practice.checkboxRadiobutton;

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 isSelectedEx 
{
   public static void main(String[] args) throws InterruptedException 
   {

      WebDriver driver;
      System.setProperty("webdriver.chrome.driver","Path Of Browser Driver");

      //When The Checkboxes have an Unique ID
      driver=new ChromeDriver();
      driver.manage().window().maximize();
      driver.get("https://softwaretestingo.blogspot.com/2020/08/checkbox-radio-button.html");
      driver.manage().timeouts().implicitlyWait(15000, TimeUnit.SECONDS);
      
      //Locate The Element
      driver.findElement(By.id("python")).click();
      WebElement pythonElement=driver.findElement(By.id("python"));
      //If Element is Selected Then Return True else It Will Return False
      Boolean RadiobuttonValue=pythonElement.isSelected();
      if(RadiobuttonValue)
      {
         System.out.println("Pyhton Is Selected");
      }
      else
         System.out.println("Python Is Not Selected");
      Thread.sleep(2000);
      driver.close();
   }
}

I love open-source technologies and am very passionate about software development. I like to share my knowledge with others, especially on technology that's why I have given all the examples as simple as possible to understand for beginners. All the code posted on my blog is developed, compiled, and tested in my development environment. If you find any mistakes or bugs, Please drop an email to softwaretestingo.com@gmail.com, or You can join me on Linkedin.

Leave a Comment