Handling Checkboxes in Selenium WebDriver

Handling Checkboxes in Selenium WebDriver

16 July 2024 Stephan Petzl Leave a comment QA

When working with Selenium WebDriver, you may encounter situations where you need to interact with checkboxes. A common requirement is to check a checkbox if it is not already checked, or leave it as is if it is already selected. This guide will walk you through the process of handling checkboxes using Selenium WebDriver, providing practical examples to help you implement this functionality in your automated tests.

Understanding the Problem

Consider a scenario where you have a form with multiple checkboxes. You need to ensure that a specific checkbox is checked, but only if it is not already selected. This can be achieved using Selenium WebDriver by leveraging the isSelected() method to determine the current state of the checkbox.

Example Scenario

Let’s take an example where you have three checkboxes on a webpage, and you want to check the first checkbox if it is not already checked, and uncheck the third checkbox if it is checked. Here’s how you can achieve this:

Implementing the Solution

Using CSS Selectors

The following code demonstrates how to use CSS selectors to locate checkboxes and perform the required actions:

WebElement checkBox1;
WebElement checkBox3;

checkBox1 = driver.findElement(By.cssSelector("input[value='cb1']"));
checkBox3 = driver.findElement(By.cssSelector("input[value='cb3']"));

if (!checkBox1.isSelected()) {
    checkBox1.click();
}

// checkBox3 is selected by default
if (checkBox3.isSelected()) {
    checkBox3.click();
}

Using findElements Method

Alternatively, you can use the findElements method to handle multiple checkboxes:

List<WebElement> selectElements = driver.findElements(By.cssSelector("input[name='checkboxes[]']"));

selectElements.get(0).click();

if (selectElements.get(2).isSelected()) {
    selectElements.get(2).click();
}

Iterating Over Checkboxes

If you need to iterate over all checkboxes and perform actions based on their state, you can use a loop:

for (WebElement checkbox : selectElements) {
    // uncheck 'em all
    if (checkbox.isSelected()) {
        checkbox.click();
    }
}

Conclusion

Interacting with checkboxes in Selenium WebDriver is straightforward once you understand how to use the isSelected() method and appropriate selectors. By following the examples provided, you can ensure that your automated tests handle checkboxes effectively, improving the reliability and accuracy of your test scripts.

For more advanced techniques and best practices in automated testing, refer to our comprehensive guides:

Enhance Your Testing with Repeato

For those looking to streamline their mobile app testing, consider using Repeato, a no-code test automation tool for iOS and Android. Repeato allows you to create, run, and maintain automated tests quickly and efficiently. Its computer vision and AI-based approach make it particularly fast to edit and run tests, ensuring your apps are thoroughly tested with minimal effort. Learn more about how Repeato can enhance your quality assurance process in our getting started guide.

Like this article? there’s more where that came from!