0
0
Selenium Javatesting~5 mins

Radio button handling in Selenium Java

Choose your learning style9 modes available
Introduction

Radio buttons let users pick one choice from many. Testing them ensures the right option is selected and works well.

When testing a form with options like gender or payment method.
When verifying only one radio button can be selected at a time.
When checking if the default radio button is correctly pre-selected.
When ensuring clicking a radio button changes the selection properly.
When validating form submission depends on the chosen radio button.
Syntax
Selenium Java
WebElement radioButton = driver.findElement(By.id("radioButtonId"));
radioButton.click();
boolean isSelected = radioButton.isSelected();

Use unique locators like id or name to find radio buttons.

Use isSelected() to check if a radio button is chosen.

Examples
Selects the 'male' radio button and checks if it is selected.
Selenium Java
WebElement maleOption = driver.findElement(By.id("male"));
maleOption.click();
boolean selected = maleOption.isSelected();
Finds all radio buttons with name 'gender' and clicks the one with value 'female'.
Selenium Java
List<WebElement> options = driver.findElements(By.name("gender"));
for (WebElement option : options) {
    if (option.getAttribute("value").equals("female")) {
        option.click();
        break;
    }
}
Sample Program

This test opens a page with two radio buttons, clicks each one, and checks if the selection updates correctly.

Selenium Java
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class RadioButtonTest {
    public static void main(String[] args) {
        System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
        WebDriver driver = new ChromeDriver();
        try {
            driver.get("https://example.com/radio-form");

            WebElement option1 = driver.findElement(By.id("option1"));
            WebElement option2 = driver.findElement(By.id("option2"));

            // Click first radio button
            option1.click();

            // Assert option1 is selected
            if (!option1.isSelected()) {
                System.out.println("Test Failed: option1 should be selected.");
            } else {
                System.out.println("option1 is selected as expected.");
            }

            // Click second radio button
            option2.click();

            // Assert option2 is selected and option1 is not
            if (option2.isSelected() && !option1.isSelected()) {
                System.out.println("option2 is selected and option1 is not, as expected.");
            } else {
                System.out.println("Test Failed: Radio button selection did not update correctly.");
            }
        } finally {
            driver.quit();
        }
    }
}
OutputSuccess
Important Notes

Always wait for the page to load before interacting with radio buttons.

Use clear and unique locators to avoid selecting the wrong element.

Remember only one radio button in a group can be selected at a time.

Summary

Radio buttons let users select one option from many.

Use click() to select and isSelected() to check selection.

Test that selecting one radio button deselects others in the group.