Selenium Checkboxes And Radio Buttons
Checkboxes and radio buttons collect choices on forms. A checkbox works like a light switch that stays on or off, and a form may hold many switches at once. A radio button works like the station buttons on an old car radio. Pressing one button releases the others, so only one choice stays active. Selenium clicks both controls and reads their state, which lets a test confirm that a form behaves as the designer planned.
Control Comparison
| Feature | Checkbox | Radio Button |
|---|---|---|
| Choices allowed | Zero, one, or many | Exactly one per group |
| Click on a selected item | Turns it off | Keeps it selected |
| Typical use | Interests, terms agreement | Gender, payment method |
| HTML type | input type="checkbox" | input type="radio" |
Picture of a Form
Newsletter topics (checkboxes) Delivery speed (radio buttons) [x] Sports ( ) Standard [ ] Music (o) Express [x] Travel ( ) Same day Many boxes can hold a tick. One circle holds the dot.
Three State Methods
Every checkbox and radio button answers three yes-or-no questions.
| Method | Question Answered |
|---|---|
| isSelected() | Does the control hold a tick or dot? |
| isEnabled() | Can the user interact with it? |
| isDisplayed() | Does the user see it on screen? |
Handling a Single Checkbox
A click toggles the state. A careless test that clicks twice ends up with the original state, so check the current state before clicking.
WebElement sports = driver.findElement(By.id("sports"));
if (!sports.isSelected()) {
sports.click();
}
System.out.println("Sports ticked: " + sports.isSelected());Handling a Group of Checkboxes
The findElements method returns every matching box. A loop ticks each one or picks boxes by value.
List<WebElement> boxes = driver.findElements(By.cssSelector("input[type='checkbox']"));
for (WebElement box : boxes) {
if (!box.isSelected()) {
box.click();
}
}
Before loop: [x] [ ] [x] [ ] After loop : [x] [x] [x] [x]
A selective version reads the value attribute and clicks only the wanted choices.
for (WebElement box : boxes) {
String value = box.getAttribute("value");
if (value.equals("Music") || value.equals("Travel")) {
box.click();
}
}Handling Radio Buttons
Radio buttons in one group share the same name attribute. Locate the group, read each value, and click the one you need.
List<WebElement> speeds = driver.findElements(By.name("delivery"));
for (WebElement speed : speeds) {
if (speed.getAttribute("value").equals("express")) {
speed.click();
break;
}
}A direct locator works when the radio button has its own id.
driver.findElement(By.id("express")).click();Verifying Radio Group Behavior
A good test proves the one-choice rule. Click the first option, confirm it holds the dot, click the second option, and confirm that the first option lost the dot.
Click Standard -> Standard selected, Express not selected Click Express -> Express selected, Standard not selected
Assert.assertTrue(standard.isSelected());
express.click();
Assert.assertFalse(standard.isSelected());
Assert.assertTrue(express.isSelected());Clicking Hidden Controls
Modern designs hide the real input and show a styled label or icon instead. A direct click on the hidden input raises an ElementNotInteractableException. Click the visible label that sits beside the input, and read the state from the hidden input.
driver.findElement(By.cssSelector("label[for='terms']")).click();
Assert.assertTrue(driver.findElement(By.id("terms")).isSelected());Disabled Controls
Some options stay locked until the user meets a condition, such as an Express choice that unlocks after the cart passes a minimum amount. Check isEnabled() before the click, and assert the disabled state when a rule demands it.
Practical Test Ideas
- Confirm that the terms checkbox starts unticked.
- Confirm that the Submit button stays disabled until the terms box holds a tick.
- Confirm that Select All ticks every item box.
- Confirm that unticking one item clears the Select All box.
- Confirm that the chosen radio value reaches the summary page.
Key Points
- Check the current state before clicking a checkbox.
- Use findElements to process a group.
- Radio buttons in one group share a name.
- Click the label when the input stays hidden.
Real Form Scenario: Event Registration
A registration page asks visitors to choose workshops with checkboxes and a ticket type with radio buttons. A complete test ticks two workshops, picks the Student ticket, and submits the form. The confirmation page must list both workshops and the Student price. The test reads the text of each selected control through its label, stores the values in a list, and compares that list with the confirmation text. This approach checks the whole journey from form to summary instead of only the click.
Reading Labels of Selected Items
Users see label text, not value attributes. A helper method collects the visible names of every ticked box, which makes failure messages easy to read.
List<String> chosen = new ArrayList<>();
for (WebElement box : boxes) {
if (box.isSelected()) {
chosen.add(box.findElement(By.xpath("./following-sibling::label")).getText());
}
}
System.out.println("Selected: " + chosen);