![]() |
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-
1. How to Locate a CheckboxSelenium provides multiple locators for locating a checkbox, some of them are-
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
Python
Here if we have successfully located the element the Process finished with exit code 0 else it will throw a NoSuchElementException exception. Output: ![]() Selenium Handling Checkbox ![]() Selenium Handling Checkbox 2. How to check if the Checkbox is Selected or Not?Selenium has a special method to check if a checkbox is selected or not
Python
Output: False ![]() Selenium Handling Checkbox The output of the following will be false as we have not selected the checkbox ![]() Selenium Handling Checkbox 3. How to select or deselect a checkbox?Once we find the checkbox, the next thing we want to do is select or deselect the element. But to select or de-select an element we first have to check if it’s enabled or not. Here are the methods which will help us do so.
Python
Output: selected Here in the if block we check if our checkbox is enabled or not, and if it is enable we use the click() method to select the checkbox. ![]() Selenium Handling Checkbox ![]() Selenium Handling Checkbox 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 WebDriverSTEP-1 Locate the CheckBox Selenium provides multiple locators for locating a checkbox, some of them are-
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
Here if we have successfully located the element the Process finished with exit code 0 else it will throw a NoSuchElementException exception. Output: ![]() Selenium Handling Checkbox ![]() Selenium Handling Checkbox STEP-2. Perform Actions on the Checkbox.To interact with the checkbox we can use the ‘click()‘ method to toggle its state. Python
The checkbox will be selected if it’s not selected and vice versa. Output: ![]() Selenium Handling Checkbox STEP-3. Validate the Checkbox StateAfter performing actions on the checkbox we may want to validate its state. Selenium has a special method to validate the state of the checkbox.
Python
![]() Selenium Handling Checkbox Output: If the checkbox is selected it will print “checkbox is selected” in the console ![]() Selenium Handling Checkbox |
Reffered: https://www.geeksforgeeks.org
Automation Testing |
Related |
---|
![]() |
|
![]() |
![]() |
![]() |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 9 |