Radio button handling in Selenium Java - Build an Automation Script
import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.support.ui.ExpectedConditions; import org.openqa.selenium.support.ui.WebDriverWait; import java.time.Duration; import static org.junit.jupiter.api.Assertions.assertTrue; public class RadioButtonTest { private WebDriver driver; private WebDriverWait wait; @BeforeEach public void setUp() { // Set path to chromedriver if needed driver = new ChromeDriver(); wait = new WebDriverWait(driver, Duration.ofSeconds(10)); } @Test public void testSelectMaleRadioButton() { driver.get("https://example.com/webform"); // Wait until the radio button is clickable WebElement maleRadio = wait.until( ExpectedConditions.elementToBeClickable(By.id("gender_male")) ); maleRadio.click(); // Assert the radio button is selected assertTrue(maleRadio.isSelected(), "Male radio button should be selected after clicking"); } @AfterEach public void tearDown() { if (driver != null) { driver.quit(); } } }
This test class uses Selenium WebDriver with JUnit 5 to automate the radio button selection.
setUp(): Initializes ChromeDriver and WebDriverWait before each test.
testSelectMaleRadioButton(): Navigates to the web form page, waits explicitly for the radio button with id 'gender_male' to be clickable, clicks it, then asserts it is selected.
tearDown(): Closes the browser after the test to clean up.
Explicit waits ensure the element is ready before interaction, and using By.id is a best practice for locating elements reliably. The assertion confirms the radio button state matches expected behavior.
Now add data-driven testing to select and verify three different radio buttons with ids 'gender_male', 'gender_female', and 'gender_other'.
Practice
isSelected() method do when used on a radio button element in Selenium?Solution
Step 1: Understand the purpose of
This method checks the current state of a radio button or checkbox to see if it is selected or not.isSelected()Step 2: Differentiate from other methods
click()selects the radio button, butisSelected()only checks selection status without changing it.Final Answer:
Checks if the radio button is currently selected -> Option AQuick Check:
isSelected()= Check selection status [OK]
- Confusing isSelected() with click()
- Expecting isSelected() to select the button
- Using getText() to check selection
Solution
Step 1: Identify the method to select a radio button
To select a radio button, you use theclick()method on the WebElement.Step 2: Check other options for correctness
isSelected()only checks selection,getText()returns label text, andsendKeys()is not used for radio buttons.Final Answer:
driver.findElement(By.id("option1")).click(); -> Option DQuick Check:
Useclick()to select radio button [OK]
- Using isSelected() to select instead of check
- Trying to sendKeys() to radio buttons
- Using getText() expecting to select
WebElement rb1 = driver.findElement(By.id("radio1"));
WebElement rb2 = driver.findElement(By.id("radio2"));
rb1.click();
boolean selected1 = rb1.isSelected();
boolean selected2 = rb2.isSelected();
What will be the values of selected1 and selected2 after execution?Solution
Step 1: Understand radio button behavior on click
Clickingrb1selects it and deselects other radio buttons in the same group, sorb1.isSelected()returns true andrb2.isSelected()returns false.Step 2: Confirm the boolean values
Therefore,selected1is true andselected2is false after clickingrb1.Final Answer:
selected1 = true, selected2 = false -> Option AQuick Check:
Click selects one, others deselect = true/false [OK]
- Assuming both radio buttons can be selected simultaneously
- Confusing isSelected() return values
- Not understanding radio button group behavior
WebElement radio = driver.findElement(By.name("gender"));
radio.click();But the test fails because the wrong radio button is selected. What is the likely problem?
Solution
Step 1: Analyze locator usage
UsingBy.name("gender")returns the first matching element, but if multiple radio buttons share the same name, this may select the wrong one.Step 2: Fix by specifying correct element
To select the intended radio button, use a more specific locator or find all elements and choose the correct one.Final Answer:
UsingBy.namereturns multiple elements; need to specify which one -> Option CQuick Check:
Locator must uniquely identify radio button [OK]
- Assuming findElement returns all matching elements
- Thinking click() doesn't work on radio buttons
- Ignoring that multiple radios share the same name attribute
Solution
Step 1: Understand radio button group behavior
Only one radio button in a group can be selected at a time; selecting one deselects others.Step 2: Write assertions to verify this
Click one radio button, then check that it is selected and all others are not selected to confirm correct behavior.Final Answer:
Click one radio button, then assert it is selected and all others are not selected -> Option BQuick Check:
Test selection exclusivity by asserting one selected, others not [OK]
- Expecting multiple radio buttons to be selected simultaneously
- Not asserting other buttons are deselected
- Using sendKeys() instead of click()
Ready to prove it?
Pass the test and this pattern moves unknown
