In this tutorial, we will see how to identify the form elements like CheckBox and Radio Button
Toggling a CheckBox or Radio Button on/off is also done using the click() method.

IsSelected – IsSelected method, let us know that the element selected or not. Assume there are two Radio Buttons/Check Boxes, one selected by default and you want to select the other one for your test. Then, we use IsSelected method. When there is a group of Radio Buttons/Check Boxes on the page then, most probably, their names are same, but values are different. Then we use the Webdriver findElements command to get the list of web elements.
Below is the working example of CheckBox.
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class Checkbox_Test {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver","C:\\Users\\Vibha\\Desktop\\Drivers\\chromedriver_win32\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
driver.get("https://www.seleniumeasy.com/test/basic-checkbox-demo.html");
//Single option selection
System.out.println("*******Single option selection *********");
driver.findElement(By.id("isAgeSelected")).click();
String Message = driver.findElement(By.id("txtAge")).getText();
System.out.println("Message is :"+Message);
// close the web browser
driver.close();
}
}
Output
*******Single option selection *********
Message is :Success - Check box is checked

How to click all options in the Checkbox
In the below image, there are 4 checkboxes. Initially, when checkboxes were not selected, the test mentioned on the button is – Check All and once all the options are checked, the text on button changes to – Uncheck All.

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 classCheckbox_Test {
public static voidmain(String[] args) throws Exception {
System.setProperty("webdriver.chrome.driver","C:\\Users\\Vibha\\Desktop\\Drivers\\chromedriver_win32\\chromedriver.exe");
WebDriver driver = newChromeDriver();
driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
driver.get("https://www.seleniumeasy.com/test/basic-checkbox-demo.html");
//Display the value of Button before clicking the options of checkbox
String buttontext_beforeclick = driver.findElement(By.xpath(".//*[@id='check1']")).getAttribute("value");
System.out.println("Text before click :"+buttontext_beforeclick);
// Find the CheckBox by its classname
List list = driver.findElements(By.xpath("//input[@type ='checkbox' and @class='cb1-element']") );
// Get the number of CheckBoxes available
int CheckBox_Size = list.size();
System.out.println("Number of Checkbox options :"+CheckBox_Size);
// Iterate the loop from first CheckBox to last Checkbox
for(int i=0;i<CheckBox_Size;i++)
{
list.get(i).click();
}
String buttontext_afterclick = driver.findElement(By.xpath(".//*[@id='check1']")).getAttribute("value");
System.out.println("Text after click :"+buttontext_afterclick);
driver.quit();
}
}
Output
Text before click :Check All
Number of Checkbox options :4
Text after click :Uncheck All

Similarly, below I have explained Radio Button.

import java.util.List;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
public class Radio_Test {
public static void main(String[] args) {
System.setProperty("webdriver.gecko.driver","C:\\Users\\vibha\\Downloads\\geckodriver-v0.24.0-win64\\geckodriver.exe");
FirefoxDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
driver.get("https://www.seleniumeasy.com/test/basic-radiobutton-demo.html");
List<WebElement> Radio_Options = driver.findElements(By.name("optradio"));
// 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();
//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"));
}
driver.findElement(By.id("buttoncheck")).click();
String Button_Selected = driver.findElement(By.xpath("//*[@id='easycont']/div/div[2]/div[1]/div[2]/p[3]")).getText();
System.out.println("Get Checked Value is :"+ Button_Selected);
//Group Radio Button Selection
driver.findElement(By.xpath(".//input[@name='gender'and @value='Female']")).click();
driver.findElement(By.xpath(".//input[@name='ageGroup' and @value='15 - 50']")).click();
driver.findElement(By.xpath(".//*[@id='easycont']/div/div[2]/div[2]/div[2]/button")).click();
String Group_Radio_Message = driver.findElement(By.xpath("//*[@id='easycont']/div/div[2]/div[2]/div[2]/p[2]")).getText();
System.out.println("Get Values are :"+Group_Radio_Message);
// close the web browser
driver.close();
}
}
Output
Button Selected is :Male
Get Checked Value is :Radio button 'Male' is checked
Get Values are :Sex : Female
Age group: 15 - 50