π What is a Checkbox?
A CheckBox is a GUI (Graphical User Interface) element that allows users to select one or multiple options from a set of choices. Each checkbox operates independently, meaning users can check or uncheck multiple checkboxes without affecting others.
π HTML Checkbox Syntax:
<input type="checkbox" id="accept" name="terms" value="Accepted">
π How to Handle Checkboxes in Selenium WebDriver?
Selenium provides different ways to interact with checkboxes using methods like:
β
click()
β To select or deselect a checkbox.
β
isSelected()
β To check if a checkbox is selected.
β
isDisplayed()
β To verify if a checkbox is visible.
β
isEnabled()
β To check if a checkbox is active and interactable.
π₯οΈ Locating and Selecting a Checkbox in Selenium
π― 1. Using ID Locator
If a checkbox has an id
attribute, the easiest way to locate it is by using By.id()
.
πΉ Example:
// Locate the checkbox using ID
WebElement checkbox = driver.findElement(By.id("accept"));
// Click the checkbox to select it
checkbox.click();
β
This method is fast and reliable but only works if the checkbox has a unique id
.
π― 2. Using XPath Locator
If there is no id
, we can use XPath to locate the checkbox.
πΉ Example: Selecting a Checkbox with XPath
// Locate the checkbox using XPath
WebElement checkbox = driver.findElement(By.xpath("//input[@type='checkbox' and @name='terms']"));
// Click to select it
checkbox.click();
β
Why use XPath?
β Works even if id
is missing.
β Can be used for complex elements.
π― 3. Using CSS Selector Locator
Another way to locate checkboxes is by using CSS Selectors.
πΉ Example: Selecting a Checkbox with CSS Selector
// Locate checkbox using CSS Selector
WebElement checkbox = driver.findElement(By.cssSelector("input[type='checkbox'][name='terms']"));
// Click to select it
checkbox.click();
β
Why use CSS Selector?
β Faster than XPath.
β Works well for dynamic elements.
β Performing Validations on a Checkbox in Selenium WebDriver
π 1. Using isSelected()
Method
This method checks if a checkbox is selected (ticked β ).
πΉ Example: Check if the Checkbox is Selected
if (checkbox.isSelected()) {
System.out.println("β
Checkbox is selected.");
} else {
System.out.println("β Checkbox is NOT selected.");
}
β Useful when you want to verify user selections.
π 2. Using isDisplayed()
Method
This method checks if a checkbox is visible on the webpage.
πΉ Example: Check if the Checkbox is Displayed
if (checkbox.isDisplayed()) {
System.out.println("π Checkbox is visible on the page.");
} else {
System.out.println("β Checkbox is hidden.");
}
β Useful when elements are dynamically loaded.
π 3. Using isEnabled()
Method
This method checks if a checkbox is enabled (active for clicking).
πΉ Example: Check if the Checkbox is Enabled
if (checkbox.isEnabled()) {
System.out.println("β
Checkbox is enabled and can be clicked.");
} else {
System.out.println("β Checkbox is disabled.");
}
β Useful when some checkboxes are disabled by default.
π Handling Multiple Checkboxes in Selenium
If there are multiple checkboxes, we can select them using a loop.
πΉ Example: Select All Checkboxes on the Page
List<WebElement> checkboxes = driver.findElements(By.xpath("//input[@type='checkbox']"));
for (WebElement cb : checkboxes) {
if (!cb.isSelected()) {
cb.click(); // Select if not already selected
}
}
πΉ Example: Select a Specific Checkbox by Value
String valueToSelect = "Selenium";
List<WebElement> checkboxes = driver.findElements(By.xpath("//input[@type='checkbox']"));
for (WebElement cb : checkboxes) {
if (cb.getAttribute("value").equals(valueToSelect)) {
cb.click();
}
}
β This is useful when checkboxes are generated dynamically.
π― Final Summary
β Locating Checkboxes: Use id
, XPath
, or CSS Selector
.
β Selecting a Checkbox: Use click()
.
β Validation Methods:
isSelected()
β Checks if the checkbox is selected.isDisplayed()
β Checks if the checkbox is visible.isEnabled()
β Checks if the checkbox is active.
β Handling Multiple Checkboxes: Use loops to select multiple checkboxes.
By mastering these methods, you can efficiently handle checkbox elements in Selenium WebDriver! ππ―