About this lesson
Handle radio buttons in Selenium Java: select one option and check the group
Selenium Java AssignmentsIntermediateCheckboxes & Radio Buttons
How it works
Radio buttons with the same name form a group, and the browser lets only one of them be selected at a time. Selecting one clears the others, which is the behaviour this lesson checks. On the Radio Buttons page the first row's three buttons all have name="select", so they are one group.
Unlike a checkbox, a radio cannot be cleared by clicking it again. A second click on a selected radio leaves it selected. The only way to clear it through the page is to select another radio in the same group.
The second row looks the same but each radio has a different name, so each one is a group of its own and all three can be selected together. Whether radios exclude each other depends on the name, not on the layout.
About the assignment
The starter gives you the first and second radios of the first row. You select the first, then the second, and print the state of both, so the output shows the group clearing the first radio when the second is chosen.
What you will do
- Print the first radio's
isSelected()with the labelFirst initially:. - Click the first radio and print its state with the label
First after click:. - Click the second radio.
- Print the state of both radios using the two labels from the expected output, first radio first.
Common mistakes
Using a stored boolean
A
booleansaved before the second click still saystrue. CallisSelected()again after the click to see the cleared state.Clicking again to clear a radio
The radio stays selected and no exception is thrown, so a test that expects it to clear fails on the assertion, not on the click.
Clicking the disabled or hidden rows
The disabled row ignores the click without an exception, and the hidden row throws
ElementNotInteractableException.
In a real test suite
Payment method, shipping speed and survey answers are usually radio groups. A good test selects one option and then asserts what depends on it, such as the price or the next form section, rather than only the radio's own state.
Lesson 23 unticked a checkbox, something radios cannot do. Lesson 25 goes back to the Checkboxes page to detect controls that are disabled before you try them.
Related lessons
- Handle Checkboxes - Click and VerifyCheckboxes, which toggle instead of excluding each other.
- Find Element by NameLocate elements by their
nameattribute, the attribute that ties a radio group together. - Find Multiple Elements with findElements()Collect every radio on this page with
findElements().
To try one of these calls on any page without a task, open the online Selenium compiler for Java.
Starter code
The editor above opens with this code. Fill in the TODO lines, then run and submit it.
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
public class Main {
public static void main(String[] args) throws Exception {
WebDriver driver = new ChromeDriver();
driver.get("https://testkru.com/Elements/RadioButtons");
WebElement first = driver.findElement(By.id("firstSelect1"));
WebElement second = driver.findElement(By.id("secondSelect1"));
// TODO: Print first radio's initial state
// TODO: Click first radio, print its state
// TODO: Click second radio, print both states
driver.quit();
}
}