---
description: In this tutorial, we will see how to identify the following form elements Radio Button Check Box Radio Button Radio Buttons too can be toggled on by using the click() method. Using http://demo.guru99.
title: Select Radio Button and CheckBox in Selenium
image: https://www.guru99.com/images/handle-radio-button-checkbox-in-selenium-webdriver.png
---

[Skip to content](#main)

**⚡ 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.

Read More

![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]()](https://www.guru99.com/images/2-2017/072717_0632_SeleniumWeb8.png)

## 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]()](https://www.guru99.com/images/2-2017/072717_0632_SeleniumWeb9.png)

[![Console output showing checkbox toggle state]()](https://www.guru99.com/images/2-2017/072717_0632_SeleniumWeb10.png)

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()]()](https://www.guru99.com/images/2-2017/072717_0632_SeleniumWeb11.png)

## 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();
    }
}
```

**Don't Miss:**

- [Action Class in Selenium](https://www.guru99.com/keyboard-mouse-events-files-webdriver.html)
- [Selenium Tutorial – Guru99](https://www.guru99.com/selenium-tutorial.html)
- [DataProvider in TestNG: Selenium Parameterization Example](https://www.guru99.com/parameterization-using-xml-and-dataproviders-selenium.html)
- [TestNG Reports Generation in Selenium: How to Generate?](https://www.guru99.com/testng-report.html)

## 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](https://www.guru99.com/select-option-dropdown-selenium-webdriver.html). 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

⚡ How do I deselect a checkbox in Selenium?

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.

🚀 What is the difference between isSelected() and isEnabled() in Selenium?

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.

💡 How do I select all checkboxes on a page in Selenium?

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.

🤖 How can AI help automate radio buttons and checkboxes in Selenium?

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.

🧠 Can AI detect dynamic checkbox or radio button locators automatically?

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:

ChatGPTPerplexityGrokGoogle AI

**Stay Updated on AI** **Get Weekly AI Skills, Trends, Actionable Advice.**

##### Sign up for the newsletter

Subscribe for Free

You have successfully subscribed.  
Please check your inbox.

![AI-Newsletter]() Chosen by over **350,000+** professionals

[Scroll to top](#wrapper)

×

Search for:

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://www.guru99.com/#organization","name":"Guru99","sameAs":["https://www.facebook.com/Guru99Official","https://twitter.com/guru99com"],"logo":{"@type":"ImageObject","@id":"https://www.guru99.com/#logo","url":"https://www.guru99.com/images/guru99-logo-v1-150x59.png","contentUrl":"https://www.guru99.com/images/guru99-logo-v1-150x59.png","caption":"Guru99","inLanguage":"en-US"}},{"@type":"WebSite","@id":"https://www.guru99.com/#website","url":"https://www.guru99.com","name":"Guru99","publisher":{"@id":"https://www.guru99.com/#organization"},"inLanguage":"en-US"},{"@type":"ImageObject","@id":"https://www.guru99.com/images/handle-radio-button-checkbox-in-selenium-webdriver.png","url":"https://www.guru99.com/images/handle-radio-button-checkbox-in-selenium-webdriver.png","width":"700","height":"250","caption":"Handle Radio Button &amp; CheckBox in Selenium WebDriver","inLanguage":"en-US"},{"@type":"BreadcrumbList","@id":"https://www.guru99.com/checkbox-and-radio-button-webdriver.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":"1","item":{"@id":"https://www.guru99.com","name":"Home"}},{"@type":"ListItem","position":"2","item":{"@id":"https://www.guru99.com/selenium","name":"Selenium"}},{"@type":"ListItem","position":"3","item":{"@id":"https://www.guru99.com/checkbox-and-radio-button-webdriver.html","name":"Select Radio Button and CheckBox in Selenium"}}]},{"@type":"WebPage","@id":"https://www.guru99.com/checkbox-and-radio-button-webdriver.html#webpage","url":"https://www.guru99.com/checkbox-and-radio-button-webdriver.html","name":"Select Radio Button and CheckBox in Selenium","dateModified":"2026-06-17T18:49:56+05:30","isPartOf":{"@id":"https://www.guru99.com/#website"},"primaryImageOfPage":{"@id":"https://www.guru99.com/images/handle-radio-button-checkbox-in-selenium-webdriver.png"},"inLanguage":"en-US","breadcrumb":{"@id":"https://www.guru99.com/checkbox-and-radio-button-webdriver.html#breadcrumb"}},{"@type":"Person","@id":"https://www.guru99.com/author/admin","name":"Krishna Rungta","url":"https://www.guru99.com/author/admin","image":{"@type":"ImageObject","@id":"https://www.guru99.com/images/krishna-rungta-v2-120x120.png","url":"https://www.guru99.com/images/krishna-rungta-v2-120x120.png","caption":"Krishna Rungta","inLanguage":"en-US"},"worksFor":{"@id":"https://www.guru99.com/#organization"}},{"articleSection":"Selenium","headline":"Select Radio Button and CheckBox in Selenium","description":"In this tutorial, we will see how to identify the following form elements Radio Button Check Box Radio Button Radio Buttons too can be toggled on by using the click() method. Using http://demo.guru99.","keywords":"selenium","speakable":{"@type":"SpeakableSpecification","cssSelector":[".entry-title",".summary"]},"@type":"Article","author":{"@id":"https://www.guru99.com/author/admin","name":"Krishna Rungta"},"dateModified":"2026-06-17T18:49:56+05:30","image":{"@id":"https://www.guru99.com/images/handle-radio-button-checkbox-in-selenium-webdriver.png"},"copyrightYear":"2026","name":"Select Radio Button and CheckBox in Selenium","subjectOf":[{"@type":"FAQPage","mainEntity":[{"@type":"Question","name":"How do I deselect a checkbox in Selenium?","acceptedAnswer":{"@type":"Answer","text":"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."}},{"@type":"Question","name":"What is the difference between isSelected() and isEnabled() in Selenium?","acceptedAnswer":{"@type":"Answer","text":"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."}},{"@type":"Question","name":"How do I select all checkboxes on a page in Selenium?","acceptedAnswer":{"@type":"Answer","text":"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."}},{"@type":"Question","name":"How can AI help automate radio buttons and checkboxes in Selenium?","acceptedAnswer":{"@type":"Answer","text":"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."}},{"@type":"Question","name":"Can AI detect dynamic checkbox or radio button locators automatically?","acceptedAnswer":{"@type":"Answer","text":"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."}}]}],"@id":"https://www.guru99.com/checkbox-and-radio-button-webdriver.html#schema-1115964","isPartOf":{"@id":"https://www.guru99.com/checkbox-and-radio-button-webdriver.html#webpage"},"publisher":{"@id":"https://www.guru99.com/#organization"},"inLanguage":"en-US","mainEntityOfPage":{"@id":"https://www.guru99.com/checkbox-and-radio-button-webdriver.html#webpage"}}]}
```
