Select Radio Button and CheckBox in Selenium
โก Smart Summary
Select Radio Button and CheckBox in Selenium covers the WebDriver techniques for automating two of the most common form controls. This tutorial explains how the click() and isSelected() methods toggle radios and checkboxes, walks through working Java code, and shares troubleshooting tips for fragile locators.

Radio Button in Selenium
Radio buttons are toggled on using the click() method, just like any other WebElement in Selenium. Because only one option in a radio group can be active at a time, clicking a second option automatically deselects the first.
Using https://demo.guru99.com/test/radio.html as a practice page, note that radio1.click() selects the “Option1” radio button. Calling radio2.click() then selects “Option2” while leaving “Option1” unselected.
How to Select a Checkbox in Selenium
Toggling a checkbox on or off is also done using the click() method. The code below clicks Facebook’s “Keep me logged in” checkbox twice and prints TRUE when the checkbox is toggled on and FALSE when it is toggled off.
The isSelected() method is used to confirm whether a checkbox is toggled on or off โ a key assertion when validating form state in test cases.
Here is another example page: https://demo.guru99.com/test/radio.html.
Complete Code Example
Here is the complete working code that toggles both radio buttons and the Facebook persist-login checkbox.
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.*;
public class Form {
public static void main(String[] args) {
// Declaration and instantiation of objects and variables
System.setProperty("webdriver.chrome.driver", "G:\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://demo.guru99.com/test/radio.html");
WebElement radio1 = driver.findElement(By.id("vfb-7-1"));
WebElement radio2 = driver.findElement(By.id("vfb-7-2"));
// Radio Button 1 is selected
radio1.click();
System.out.println("Radio Button Option 1 Selected");
// Radio Button 1 is de-selected and Radio Button 2 is selected
radio2.click();
System.out.println("Radio Button Option 2 Selected");
// Selecting Checkbox
WebElement option1 = driver.findElement(By.id("vfb-6-0"));
// This will toggle the checkbox
option1.click();
// Check whether the checkbox is toggled on
if (option1.isSelected()) {
System.out.println("Checkbox is Toggled On");
} else {
System.out.println("Checkbox is Toggled Off");
}
// Selecting a Checkbox and using the isSelected method
driver.get("https://demo.guru99.com/test/facebook.html");
WebElement chkFBPersist = driver.findElement(By.id("persist_box"));
for (int i = 0; i < 2; i++) {
chkFBPersist.click();
System.out.println("Facebook Persists Checkbox Status is - " + chkFBPersist.isSelected());
}
// driver.close();
}
}
Troubleshooting
If you encounter NoSuchElementException while finding elements, it means the element was not present at the moment the WebDriver accessed the page. Use the checklist below to recover quickly:
- Check your locator again using FirePath or Inspect Element in Chrome.
- Confirm that the value you used in the code matches the current value of the element on the page.
- Some elements have dynamic attributes, similar to what you may see when you select a dropdown in Selenium WebDriver. In those cases, prefer
By.xpath()orBy.cssSelector(), which are more reliable but slightly more complex. - Sometimes the issue is timing โ the WebDriver executed your code before the page finished loading.
- Add a wait before
findElement()using implicit or explicit waits to give the element time to render.




