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 Toggle: Use the click() method on the WebElement to select a radio button; selecting another option automatically deselects the previous one.
  • โ˜‘๏ธ Checkbox Toggle: The same click() method toggles a checkbox on and off; calling it twice returns the box to its original state.
  • โœ… State Verification: Call isSelected() to confirm the current toggle state and assert TRUE for checked and FALSE for unchecked controls.
  • ๐Ÿงช Working Java Example: The complete program drives the Guru99 demo and Facebook persist-login checkbox to demonstrate both flows.
  • ๐Ÿ› ๏ธ Reliable Locators: Switch to By.xpath() or By.cssSelector() when ids change dynamically and add explicit waits to prevent NoSuchElementException.

Select Radio Button and CheckBox in Selenium

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.

Radio button selection on the Guru99 demo page

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.

Selenium code clicking the Facebook Keep me logged in checkbox

Console output showing checkbox toggle state

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.

Selenium code verifying checkbox state with isSelected()

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:

  1. Check your locator again using FirePath or Inspect Element in Chrome.
  2. Confirm that the value you used in the code matches the current value of the element on the page.
  3. 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() or By.cssSelector(), which are more reliable but slightly more complex.
  4. Sometimes the issue is timing โ€” the WebDriver executed your code before the page finished loading.
  5. Add a wait before findElement() using implicit or explicit waits to give the element time to render.

FAQs

Call click() on the same checkbox WebElement to toggle it off. There is no dedicated deselect method. Use isSelected() before clicking if you want to deselect only when the checkbox is currently checked, which keeps test logic safe.

isSelected() returns true when a checkbox or radio button is checked. isEnabled() returns true when an element can receive user input. They serve different purposes: isSelected() validates state, while isEnabled() validates whether the element accepts interaction.

Use findElements() with a locator such as By.xpath(“//input[@type=’checkbox’]”) to get all checkbox WebElements, loop through the list, check isSelected(), and call click() only on those that are unchecked. This avoids accidentally deselecting already-checked boxes.

AI assistants generate locator strategies, self-heal selectors when ids change, and suggest assertions for radio and checkbox state. AI tools also auto-generate end-to-end form-fill tests from screen recordings, reducing manual coding and test maintenance effort.

Yes. AI-powered Selenium tools analyze the DOM, learn stable attributes, and rewrite XPath or CSS selectors when ids change between releases. This self-healing capability keeps form-control tests passing without manual locator updates after every UI tweak.

Summarize this post with: