Handling Checkbox in Selenium in 2026

Learn how to select, deselect, and validate single or multiple checkbox options in Selenium with clear examples.

How to handle Checkbox in Selenium
Home Guide Handling Checkbox in Selenium in 2026

Handling Checkbox in Selenium in 2026

Ever wondered why a checkbox test passes locally but behaves inconsistently in another browser?

Like many testers, I assumed checkbox handling in Selenium was as simple as locating an element and clicking it.

That belief changed when a test that worked on my system failed on another browser, clicks weren’t registered, or the checkbox state didn’t update as expected. Adding waits and revisiting locators didn’t help.

Understanding how to properly handle, verify, and deselect checkboxes in Selenium, and validating those interactions on real browsers, can save hours of debugging.

Overview

A checkbox is a UI element that allows users to select one or more options. In Selenium, checkboxes are handled like standard web elements and can be selected, verified, or deselected using WebDriver actions.

Key Methods to Handle Checkboxes in Selenium

  • click() – Selects or deselects the checkbox
  • isSelected() – Verifies whether the checkbox is checked
  • isEnabled() – Checks if the checkbox is interactive
  • isDisplayed() – Confirms whether the checkbox is visible on the page

Locating Checkboxes in Selenium

Checkboxes can be located using common Selenium locators such as:

  • By ID → By.id(“vehicle1”)
  • By Name → By.name(“vehicle1”)
  • By XPath → By.xpath(“//input[@id=’vehicle1′]”)
  • By CSS Selector → By.cssSelector(“input#vehicle1”)
  • By Value Attribute → By.cssSelector(“input[value=’Bike’]”)

Example Usage: Handling a Checkbox in Java

WebDriver driver = new ChromeDriver();

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




// Locate the checkbox

WebElement checkbox = driver.findElement(By.id("acceptTerms"));




// Select the checkbox if not already selected

if (!checkbox.isSelected()) {

    checkbox.click();

}




// Assert checkbox state

Assert.assertTrue(checkbox.isSelected(), "Checkbox is not selected");

Handling Multiple Checkboxes in Selenium

  • Use findElements() to locate all related checkboxes
  • Iterate through the list and apply conditions for selection
  • Verify each checkbox state using isSelected()
  • Deselect checkboxes when required based on test logic

This article covers how to handle, verify, and deselect checkboxes in Selenium

What is a Checkbox?

A checkbox, also known as a selection box or tick box, is a small interactive UI element that allows users to select one or more options on a web page.

When selected, a checkmark appears inside the box to indicate the chosen option. Clicking the checkbox again deselects it, making checkboxes ideal for selecting single or multiple values from a set of choices.

In web automation, checkboxes are treated like other interactive elements such as input fields, buttons, and links, and must be reliably automated to reflect real user behavior.

According to Sarah Thomas, a software testing expert, checkbox automation should always verify the element’s state using methods like isSelected() before and after interactions. This ensures that the checkbox state truly reflects the intended user action and helps prevent flaky test results across browsers.

Since checkbox rendering and interaction can vary across browsers and platforms, validating these interactions on real browsers is essential.

Platforms like BrowserStack Automate help ensure checkbox automation behaves consistently across different browsers and operating systems.

Struggling with flaky checkbox tests?

Checkbox behavior varies by browser. Test on real browsers to catch state and click issues early.

How to handle Checkbox in Selenium?

Before jumping to how to select/deselect a checkbox on a web page using Selenium, let us first learn how to locate a checkbox using Selenium. Selenium offers various locator strategies and almost all of them can be used to locate a check box. Let us see some one of them with an example.

Using W3Schools website to demonstrate locating checkboxes on web pages using Selenium.

W3Schools

Click on “Try it Yourself button” to land to
https://www.w3schools.com/tags/tryit.asp?filename=tryhtml5_input_type_checkbox

(Note: If the above practice page appears broken for you, you may simply search “w3schools checkbox” on Google.com to get the desired practice web page)

Locate checkbox using Id locator

If a checkbox element has an id attribute which is unique, we can use the ID locator of Selenium WebDriver to locate a checkbox. A checkbox in the DOM is defined using the input tag with the type as ‘checkbox’.

Example:

<input type="checkbox" id="vehicle1" name="vehicle1" value="Bike">

Id

You can see from the above image that the “I have a bike” checkbox has an input tag with type=’checkbox’ and id=’vehicle1’. If we need to locate this checkbox with ID locator below would be the Selenium code:

WebElement bike=driver.findElement(By.id("#vehicle1"));

Selecting Checkbox in Selenium

To select any checkbox we need to use the click() method of the WebElement interface of Selenium.

bike.click();

After the click operation, the checkbox should appear to be selected as shown in below image:

checked

Locate checkbox using name locator

If a checkbox element has a name attribute which is unique, we can use the name locator of Selenium WebDriver to locate a checkbox.

You can see from the above image that the “I have a bike” checkbox has the name=’vehicle1’. If we need to locate this checkbox with name locator below would be the Selenium code:

WebElement bike=driver.findElement(By.name("vehicle1"));

To select any checkbox we need to use the click() method of WebElement Interface of Selenium.

bike.click();

Locate checkbox using XPath locator

Sometimes Selenium cannot find a checkbox with just the id or name value or any other attribute, and therefore XPath is considered in such cases.

You can see from the above image that the “I have a bike” checkbox has an input tag with name=’vehicle1’ and id = ‘vehicle1’. If we need to locate this checkbox with XPath locator below would be the Selenium code:

WebElement bike=driver.findElement(By.xpath("//input[@name='vehicle1' and @id='vehicle1']"));

To select any checkbox we need to use the click() method of WebElement Interface of Selenium.

bike.click();

Locate checkbox using CSSSelector

Like XPath, CSSSelector can also be used if Selenium cannot find a checkbox with just the id or name or any other attribute value.

You can see from the above image that the “I have a bike” checkbox has an input tag with type=’checkbox’, id = ‘vehicle1’. If we need to locate this checkbox with CSSSelector locator below would be the Selenium code:

WebElement bike=driver.findElement(By.cssSelector ("input#vehicle1"));

To select any checkbox we need to use the click() method of WebElement Interface of Selenium.

bike.click();

Locate checkbox using Attribute value

Sometimes a group of checkboxes may have the same name or class or id value, which makes it difficult to check a single checkbox. In such cases, checkboxes can be located by using their value attribute as each checkbox has a unique value assigned to it.

value

If we need to locate “I have a bike” checkbox with value attribute we can use CSSSelector and below would be the Selenium code:

WebElement bike=driver.findElement(By.cssSelector("input[value='Bike']"));

To select any checkbox we need to use the click() method of WebElement Interface of Selenium.

bike.click();

How to Select multiple options in Checkbox using Selenium

In the above example, we learned that in some web pages, there may be a group of checkboxes which have the same name/class/id attribute and therefore choosing the checkbox with respect to their value attribute is the best option if we need to click on a single checkbox.

And if there is a need to select multiple checkboxes which have the same name/id/class attribute but different attribute values, creating different web elements and then clicking on it one by one would be cumbersome. Instead we can create a list of web elements and iterate through it sequentially and click on it.

Let us see how we can select all checkboxes in the above example with the below Selenium code.

  • First, we will create a List of web elements to store all the checkboxes.
List<WebElement> chkboxes=driver.findElements(By.cssSelector("input[type='checkbox']"));
  • Then we need to find out the size of the web element list.
int size=chkboxes.size();
  • Then using the for loop and get() method we will iterate through all the check boxes and click on it using the click() method.
chkboxes.get(i).click();

Complete code:

public class CheckBox {

public static void main(String args[]) {

     WebDriver driver=new ChromeDriver();

        driver.get("https://www.w3schools.com/tags/tryit.asp?filename=tryhtml5_input_type_checkbox");

             //Switch to the iframe which contains the checkboxes

        driver.switchTo().frame("iframeResult");

            

     List<WebElement> chkboxes=driver.findElements(By.cssSelector("input[type='checkbox']"));

     int size=chkboxes.size();

     for (int i=0; i<size; i++) {

         chkboxes.get(i).click();

     }

}

}

Validations on a checkbox using Selenium

To validate pre and post conditions of checkbox’s state, Selenium provides certain methods which are as follows:

  • isEnabled(): A pre-validation for checkbox click event to check whether the checkbox is enabled or disabled on the web page. This method returns true in case element is enabled otherwise it returns false.
  • isDisplayed(): A pre-validation for checkbox click event to check whether the checkbox is displayed on the web page or not. It returns true if the desired element is displayed on DOM otherwise it returns false.
  • isSelected(): A post-validation after the checkbox click event to check whether the checkbox is selected or not. It returns true if the element is selected, else false for deselected.

Suppose there is a scenario where the user needs to first check the visibility of the checkbox and based on the visibility it needs to be selected. So in such a condition we can use isDisplayed() method as a precondition and on the basis of its result either true or false, the checkbox can be selected.

public class CheckBox { 

public static void main(String args[]) {

     WebDriver driver = new ChromeDriver();

        driver.get("https://www.w3schools.com/tags/tryit.asp?filename=tryhtml5_input_type_checkbox");

     // Switch to the iframe which contains the checkboxes

        driver.switchTo().frame("iframeResult");

     WebElement car = driver.findElement(By.cssSelector("input#vehicle2"));  

        System.out.println("car.isDisplayed()" + car.isDisplayed());

     if(car.isDisplayed()==true) {

         car.click();        

     }else {

         System.out.println("Option is not displayed");

     }

}

}

How to assert that a checkbox is checked?

If there is a need to validate whether the checkbox is selected after selecting the desired checkbox, we can use isSelected() method which returns true if the checkbox is selected and false in either case. The Selenium code to test this scenario is as follows:

public class CheckBox {

public static void main(String args[]) {

     WebDriver driver = new ChromeDriver();

        driver.get("https://www.w3schools.com/tags/tryit.asp?filename=tryhtml5_input_type_checkbox");

     // Switch to the iframe which contains the checkboxes

        driver.switchTo().frame("iframeResult");

     WebElement car = driver.findElement(By.cssSelector("input#vehicle2"));

     car.click();

        System.out.println("car.isSelected()" + car.isSelected());

     if(car.isSelected()==true) {

         System.out.println("Option is selected");

     }else {

         System.out.println("Option is not selected");

     }

}

}

How to Deselect Checkbox in Selenium

At the start of this article we learnt that the click() method of WebElement interface is used to click a checkbox. Similarly, to deselect a checkbox, the same click() method is used.

However, in web automation it is a good practice to check the state of the checkbox before deselecting it, otherwise clicking to deselect the checkbox will select the checkbox if the checkbox was not in a selected mode previously.

public class CheckBox { 

public static void main(String args[]) {

     WebDriver driver = new ChromeDriver();

        driver.get("https://www.w3schools.com/tags/tryit.asp?filename=tryhtml5_input_type_checkbox");

   //Switch to the iframe which contains the checkboxes

        driver.switchTo().frame("iframeResult");

     WebElement car = driver.findElement(By.cssSelector("input#vehicle2"));

     //car.click();

        System.out.println("car.isSelected()" + car.isSelected());

     if (car.isSelected()) {

         car.click();

         System.out.println("Car deselected");

     }else {

         System.out.println("Car is already deselected");

     }

}

}

Ensuring Reliable Checkbox Interactions on Real Browsers with BrowserStack Automate

Checkbox interactions may appear simple, but their behavior can vary across browsers due to differences in rendering, event handling, and DOM updates.

A checkbox that responds correctly in one browser may fail to register clicks, return incorrect states, or behave inconsistently in another environment, leading to flaky automation tests.

BrowserStack Automate helps validate checkbox interactions under real-user conditions by allowing Selenium tests to run on real browsers and operating systems.

Key capabilities that support reliable checkbox automation include:

  • Real browser testing: Execute Selenium tests on real desktop and mobile browsers to catch browser-specific checkbox behavior.
  • Cross-browser coverage: Validate checkbox selection, assertion, and deselection across multiple browser versions and OS combinations.
  • Parallel execution: Run checkbox-related test scenarios simultaneously to speed up feedback and identify inconsistencies faster.
  • Debugging insights: Access screenshots, logs, and video recordings to analyze checkbox interaction failures.
  • CI/CD integration: Seamlessly integrate checkbox validation into automated pipelines for continuous quality assurance.

Try BrowserStack Now

By testing checkbox interactions on real browsers with BrowserStack Automate, teams can reduce flaky tests, improve cross-browser confidence, and ensure that checkbox automation accurately reflects real user behavior.

Conclusion

Checkboxes are simple UI elements, but automating them reliably requires careful handling of selection, verification, and deselection states.

Differences in browser behavior, rendering, and event handling can easily lead to inconsistent or flaky test results if these interactions are not validated properly.

By understanding how to work with checkboxes in Selenium and validating those interactions across real browsers, testers can build more stable and trustworthy automation.

Running checkbox tests in real-world environments helps catch browser-specific issues early and ensures that automated tests reflect actual user behavior, leading to more reliable test outcomes.

Useful Resources for Selenium

Methods, Classes, and Commands

Configuration

XPath

Locators and Selectors

Waits in Selenium

Frameworks in Selenium

Miscellaneous

Best Practices, Tips and Tricks

Design Patterns in Selenium: Page Object Model and Page Factory

Action Class

TestNG and Selenium

JUnit and Selenium

Use Cases

Types of Testing with Selenium

Tags
Selenium Selenium Webdriver Website Testing

FAQs

Browsers handle DOM events and rendering differently, which can affect how checkbox clicks and state changes are registered during automation.

You can use the isSelected() method to check whether a checkbox is checked and assert its state in your test.

Use findElements() to locate all checkboxes and iterate through them, applying selection or validation logic based on your test requirements.

Checkbox tests failing across browsers?
Browser differences cause flaky states. Validate on real browsers with BrowserStack Automate.

Get answers on our Discord Community

Join our Discord community to connect with others! Get your questions answered and stay informed.

Join Discord Community
Discord