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


