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.
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.
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">
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:
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.
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.
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
- Selenium Commands every Developer or Tester must know
- Selenium WebElement Commands
- Desired Capabilities in Selenium Webdriver
- Assert and Verify Methods in Selenium
- Understanding System setProperty in Selenium
- Select Class in Selenium : How to select a value in dropdown list?
- SendKeys in Selenium WebDriver
- getAttribute() method in Selenium: What, Why, and How to use
- How does Selenium isDisplayed() method work?
- findElement vs findElements in Selenium
- Types of Listeners in Selenium (with Code Examples)
- How to set Proxy in Firefox using Selenium WebDriver?
Configuration
- How to set up Selenium on Visual Studio
- How to configure Selenium in Eclipse
- Maven Dependency Management with Selenium
- How to Build and Execute Selenium Projects
XPath
- How to use XPath in Selenium?
- How to find element by XPath in Selenium with Example
- Top Chrome Extensions to find Xpath in Selenium
Locators and Selectors
- Locators in Selenium: A Detailed Guide
- CSS Selector in Selenium: Locate Elements with Examples
- How to Create Object Repository in Selenium
Waits in Selenium
- Wait Commands in Selenium C and C#
- Selenium Wait Commands: Implicit, Explicit, and Fluent Wait
- Understanding Selenium Timeouts
- Understanding ExpectedConditions in Selenium
- Understanding Role of Thread.sleep() in Selenium
Frameworks in Selenium
- Data Driven Framework in Selenium
- Implementing a Keyword Driven Framework for Selenium: A Practical Guide
- Hybrid Framework in Selenium
Miscellaneous
- How to create Selenium test cases
- How to set Proxy in Selenium?
- Difference between Selenium Standalone server and Selenium server
- Exception Handling in Selenium WebDriver
- How to use JavascriptExecutor in Selenium
- How to run your first Selenium test script
- Parallel Testing with Selenium
Best Practices, Tips and Tricks
- Top 5 Challenges Faced During Automation Selenium Testing
- 5 Selenium tricks to make your life easier
- 6 Things to avoid when writing Selenium Test Scripts
- Best Practices for Selenium Test Automation
- Why you should pay attention to flaky Selenium tests
- How to start with Selenium Debugging
- How to make your Selenium test cases run faster
- How to upgrade from Selenium 3 to Selenium 4
- Why you should move your testing to a Selenium Cloud?
Design Patterns in Selenium: Page Object Model and Page Factory
- Design Patterns in Selenium
- Page Object Model and Page Factory in Selenium
- Page Object Model and Page Factory in Selenium C#
- Page Object Model in Selenium and JavaScript
- Page Object Model and Page Factory in Selenium Python
Action Class
- How to handle Action class in Selenium
- How to perform Mouse Hover Action in Selenium
- Understanding Click Command in Selenium
- How to perform Double Click in Selenium?
- How to Drag and Drop in Selenium?
- How to Scroll Down or Up using Selenium Webdriver
- How To verify Tooltip Using Selenium
TestNG and Selenium
- Database Testing using Selenium and TestNG
- How to use DataProvider in Selenium and TestNG?
- All about TestNG Listeners in Selenium
- How to run parallel test cases in TestNG
- How to use TestNG Reporter Log in Selenium: Tutorial
- Prioritizing tests in TestNG with Selenium
JUnit and Selenium
- Understanding JUnit assertions for Selenium Testing with Examples
- How to run JUnit Parameterized Test in Selenium
- How to write JUnit test cases
- JUnit Testing Tutorial: JUnit in Java
- How to create JUnit Test Suite? (with Examples)
Use Cases
- Handling Login Popups in Selenium WebDriver and Java
- How to Launch Browser in Selenium
- How to handle Alerts and Popups in Selenium?
- How to get Selenium to wait for a page to load
- How to Find Element by Text in Selenium: Tutorial
- How to Read/Write Excel Data using Apache POI Selenium
- How to handle Captcha in Selenium
- How to handle multiple windows in Selenium?
- How to handle Multiple Tabs in Selenium
- How to find broken links in Selenium
- How to handle Cookies in Selenium WebDriver
- How to handle iFrame in Selenium
- How to handle Web Tables in Selenium
- How To Validate Text in PDF Files Using Selenium Automation
- Get Current URL in Selenium using Python: Tutorial
Types of Testing with Selenium
- Different Testing Levels supported by Selenium
- How to perform UI Testing with Selenium
- Regression Testing with Selenium: Tutorial
- UI Automation using Python and Selenium: Tutorial
- How to Run Visual Tests with Selenium: Tutorial
- How to perform ETL Automation using Selenium
- Cross Browser Testing in Selenium : Tutorial



