πŸ“Œ 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! πŸš€πŸŽ―