---
title: Handling the radio button and checkbox using selenium
description: "**Handling the radio button and** **checkbox using selenium** **Handling Radio Button ** ***Test Case Flow: *** 1. Launch Chrome via WebDriver. 2. Navigate to https://example.com/preferences. 3. Locate the radio button using XPath //input[@type='radio' and @value='male']. 4. Check if it is displayed, enabled,…"
url: "https://vinothqaacademy.com/docs/handling-the-radio-button-and-checkbox-using-selenium/"
updated: "2025-09-26"
category: Selenium Automation Testing
tags:
  - Selenium Automation Testing
---

# Handling the radio button and checkbox using selenium

**Handling the radio button and**

**checkbox using selenium**

**Handling Radio Button **

***Test Case Flow: ***

1. Launch Chrome via WebDriver.
2. Navigate to https://example.com/preferences.
3. Locate the radio button using XPath //input[@type='radio' and @value='male'].
4. Check if it is displayed, enabled, and not already selected
5. Select the radio button if it isn’t already
6. Verify the radio button is now selected
7. Close the browser.

***Program:***

```javascript

                import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.WebElement;

import org.openqa.selenium.chrome.ChromeDriver;

public class RadioButtonExample {

    public static void main(String[] args) throws InterruptedException {

        // 1. Point to your ChromeDriver executable

        System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver");

        // 2. Launch Chrome

        WebDriver driver = new ChromeDriver();

        try {

            // 3. Navigate to the page containing your radio buttons

            driver.get("https://example.com/preferences");

            // 4. Locate the desired radio button using an XPath locator

            //    (adjust the XPath to match your page)

            WebElement maleRadio = driver.findElement(

                By.xpath("//input[@type='radio' and @value='male']"));

            // 5. Check if it is displayed, enabled, and not already selected

            System.out.println("Radio displayed? " + maleRadio.isDisplayed());

            System.out.println("Radio enabled?   " + maleRadio.isEnabled());

            System.out.println("Initially selected? " + maleRadio.isSelected());

            // 6. Select the radio button if it isn’t already

            if (maleRadio.isDisplayed() && maleRadio.isEnabled() && !maleRadio.isSelected()) {

                maleRadio.click();

                // (demo pause)

                Thread.sleep(2000);

                // 7. Verify the radio button is now selected

                System.out.println("After click, selected? " + maleRadio.isSelected());

            }

        } finally {

            // 8. Close the browser (ends the session)

            driver.quit();

        }

    }

}
```

**Handling Checkbox  **

***Test Case Flow: ***

1. Launch Chrome via WebDriver.
2. Navigate to https://example.com/preferences.
3. Locate the checkbox using an XPath locator
4. Check if the checkbox is displayed, enabled, and its current state
5. Select the checkbox if it isn’t already checked
6. Verify the checkbox is now selected
7. Uncheck it again
8. Close the browser.

***Program:***

```javascript

                import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.WebElement;

import org.openqa.selenium.chrome.ChromeDriver;

public class CheckboxExample {

    public static void main(String[] args) throws InterruptedException {

        // 1. Point to your ChromeDriver executable

        System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver");

        // 2. Launch Chrome

        WebDriver driver = new ChromeDriver();

        try {

            // 3. Navigate to the page containing your checkbox

            driver.get("https://example.com/terms");

            // 4. Locate the checkbox using an XPath locator

            //    (adjust the XPath to match your page)

            WebElement agreeCheckbox = driver.findElement(

                By.xpath("//input[@type='checkbox' and @id='agreeTerms']"));

            // 5. Check if the checkbox is displayed, enabled, and its current state

            System.out.println("Checkbox displayed?    " + agreeCheckbox.isDisplayed());

            System.out.println("Checkbox enabled?      " + agreeCheckbox.isEnabled());

            System.out.println("Initially selected?    " + agreeCheckbox.isSelected());

            // 6. Select the checkbox if it isn’t already checked

            if (agreeCheckbox.isDisplayed() 

                    && agreeCheckbox.isEnabled() 

                    && !agreeCheckbox.isSelected()) {

                agreeCheckbox.click();

                // (demo pause so you can see it happen)

                Thread.sleep(2000);

                // 7. Verify the checkbox is now selected

                System.out.println("After click, selected? " + agreeCheckbox.isSelected());

            }

            // 8. (Optional) Uncheck it again

            if (agreeCheckbox.isSelected()) {

                agreeCheckbox.click();

                Thread.sleep(1000);

                System.out.println("After uncheck, selected? " + agreeCheckbox.isSelected());

            }

        } finally {

            // 9. Close the browser (ends the session)

            driver.quit();

        }

    }

}
```
