Skip to content
CodeKruPro

Handle Radio Buttons

24/70

Video walkthrough

Watch on YouTube

Handle Radio Buttons

Radio buttons work similarly to checkboxes, but only one can be selected at a time within a group (same name).

On the Radio Buttons page, the single-select group has radio buttons with name="select".

HTML Structure (radio group):

<!-- All radios with the same name form a group -->
<input type="radio" id="firstSelect1" name="select" value="firstSelect1">
<label for="firstSelect1">First Select</label>

<input type="radio" id="secondSelect1" name="select" value="secondSelect1">
<label for="secondSelect1">Second Select</label>

<input type="radio" id="thirdSelect1" name="select" value="thirdSelect1">
<label for="thirdSelect1">Third Select</label>

Task:

  1. Navigate to https://testkru.com/Elements/RadioButtons
  2. Check if firstSelect1 is selected (should be false)
  3. Click firstSelect1 to select it
  4. Check if firstSelect1 is now selected (should be true)
  5. Click secondSelect1 (same group) and check both states

Expected output:

First initially: false
First after click: true
After selecting second - First: false
After selecting second - Second: true

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

  1. Print the first radio's isSelected() with the label First initially:.
  2. Click the first radio and print its state with the label First after click:.
  3. Click the second radio.
  4. Print the state of both radios using the two labels from the expected output, first radio first.

Common mistakes

  • Using a stored boolean

    A boolean saved before the second click still says true. Call isSelected() 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.

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.

Main.java
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();
    }
}