Selenium Handling Checkbox
Last Updated :
24 Apr, 2025
Selenium is one of the most powerful tools for testing and automating web applications. the significance lies in its ability to perform repetitive tasks and automate web applications. It allows developers, testers, and QA professionals to interact with web applications and conduct automation tests.
One of the most common tasks in web applications is handling checkboxes in web forms. Checkboxes are graphical user interface elements that allow users to make binary choices by selecting or deselecting a small box. They are commonly used in web forms to gather user preferences or select multiple items from a list.
What is a CheckBox?
Imagine we are designing an online survey form for a website and we want to gather information about the user's hobbies. We can use checkboxes to allow users to select one or more hobbies from a list of hobbies. Checkboxes are the graphical user interface elements that allow users to select or deselect an item from a list of options, they will enable users to make binary choices either selecting or deselecting an item from a list of options. In this tutorial, we'll learn how to locate and handle checkboxes in Selenium Web Driver.
Here I'm using a sample website for practice

How to handle a Checkbox in Selenium-
To interact or handle a checkbox we first need to locate them on the web page then we check if the checkbox is selected or not and then we select or deselect a checkbox according to our need
Steps to handle checkbox in Selenium-
- Locate the Checkbox
- Check if the checkbox is Selected or Not
- Select or Deselect a Checkbox
1. How to Locate a Checkbox
Selenium provides multiple locators for locating a checkbox, some of them are-
- ID
- CLASSNAME
- XPATH
- CSS-SELECTOR
- NAME
Let's look into our sample page for a better understanding Here as we can see in our DOM, our checkbox has a class of Automation so we can use CLASS_NAME locator to locate it

find_element()- is a method in selenium web driver which is used to find elements using a locator. It returns the first element that matches the condition.
find_elements()- find_elements is similar to find_element but instead of returning the first element, find_element returns a list of all the elements that match the condition
By:
We could also use other methods to locate the element such as
- By. ID
- By.NAME
- By.XPATH
- By.CSS_SELECTOR
Python
# Importing modules
from selenium import webdriver
from selenium.common import NoSuchElementException
from selenium.webdriver.common.by import By
# Creating WebDriver instance for Chrome.
driver=webdriver.Chrome();
# Define the URL of the webpage to be tested.
url="https://artoftesting.com/samplesiteforselenium"
# Navigating to the webpage.
driver.get(url)
# Try to locate the checkbox element by its CLASS_NAME
try:
checkbox = driver.find_element(By.CLASS_NAME, "Automation")
except NoSuchElementException:
# If the element is not found, print "Not Found".
print("No Found")
else:
# If the element is found, print "Found".
print("Found")
# Close the WebDriver when done.
driver.quit()