How to Perform Validations On Checkbox using Selenium WebDriver?
Checkbox validations are important while automating web applications using Selenium. Validating checkboxes ensures that they function correctly and ensures that our web applications are working as expected. In this section, we’ll explore various techniques for performing checkbox validations using Selenium.
Steps to Perform Validations on CheckBox using Selenium WebDriver
STEP-1 Locate the CheckBox
Selenium provides multiple locators for locating a checkbox, some of them are-
- ID
- CLASSNAME
- XPATH
- CSS-SELECTOR
- NAME
Locate the checkbox using one of the following methods with a suitable locator
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
Python
# Importing necessary modules from Selenium from selenium import webdriver from selenium.common.exceptions import NoSuchElementException from selenium.webdriver.common.by import By # Initialize a Chrome WebDriver driver = webdriver.Chrome() # URL of the website url = "https://artoftesting.com/samplesiteforselenium" # Open the URL driver.get(url) try : # find an element with the class_Name checkbox = driver.find_element(By.CLASS_NAME, "Automation" ) except NoSuchElementException: # If NoSuchElementException is happened, print "Not Found" print ( "Element not found" ) else : # If element is found, print "Found" print ( "Element found" ) # Close the WebDriver. driver.quit() |
Here if we have successfully located the element the Process finished with exit code 0 else it will throw a NoSuchElementException exception.
Output:
STEP-2. Perform Actions on the Checkbox.
To interact with the checkbox we can use the ‘click()‘ method to toggle its state.
Python
# Importing the necessary libraries from selenium from selenium import webdriver from selenium.webdriver.common.by import By # Initialize a Chrome WebDriver instance driver = webdriver.Chrome() # Define the URL url = "https://artoftesting.com/samplesiteforselenium" # Open URL in the Chrome browser driver.get(url) # Locate the checkbox element by its class_Name checkbox = driver.find_element(By.CLASS_NAME, "Automation" ) # Click on the checkbox element checkbox.click() # Close browser driver.quit() |
The checkbox will be selected if it’s not selected and vice versa.
Output:
STEP-3. Validate the Checkbox State
After performing actions on the checkbox we may want to validate its state. Selenium has a special method to validate the state
of the checkbox.
- is_selected()- used to check if a checkbox is Selected or Not, return true if it’s selected else false
Python
from selenium import webdriver from selenium.webdriver.common.by import By # Step 1: Initialize the WebDriver # ***You need First appropriate WebDriver executable installed for Running Code.*** driver = webdriver.Chrome(); # Step 2: Open a web page with proper URL. url = "https://www.example.com" driver.get(url) # Most commonly used locators in Selenium: # ID Locator: By.ID # Name Locator: By.NAME # XPath Locator: By.XPATH # CSS Selector Locator: By.CSS_SELECTOR checkbox = driver.find_element(By. ID , "ID_OF_ELEMENT" ) # Replace with the actual ID # Check the checkbox checkbox.click() # Check if the checkbox is selected if (checkbox.is_selected()): print ( "selected" ) # Close the WebDriver driver.quit() |
Output: If the checkbox is selected it will print “checkbox is selected” in the console
Selenium Handling Checkbox
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.