Open In App

How to get the total number of checkboxes in a page using Selenium?

Last Updated : 02 Sep, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

In the world of automated testing, Selenium is a powerful tool that helps automate web browsers. One common task during web testing is determining the number of specific elements on a page, such as checkboxes. This can be useful for validating the presence and quantity of checkboxes on a form or any other user interface component.

In this article, we will explore how to get the total number of checkboxes on a webpage using Selenium WebDriver. We will guide you through the steps using a practical example and provide a simple code snippet to illustrate the process.

Steps to Get the Total Number of Checkboxes on a Page Using Selenium

To determine the total number of checkboxes on a webpage, you need to follow these steps:

  1. Set Up Selenium WebDriver: Ensure you have Selenium WebDriver and a browser driver installed. We will use ChromeDriver for this example.
  2. Navigate to the Webpage: Open the webpage that contains the checkboxes.
  3. Locate Checkbox Elements: Use a CSS selector to find all checkbox elements on the page.
  4. Count the Checkboxes: Retrieve and count the checkbox elements.
  5. Close the Browser: After obtaining the count, close the browser.

Here is a detailed explanation of each step with the accompanying code example:

Java
package com.example.tests;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import io.github.bonigarcia.wdm.WebDriverManager;
import java.util.List;

public class ChromeDriverTest12  {
    public static void main(String[] args) {
        // Set up ChromeDriver using WebDriverManager
        WebDriverManager.chromedriver().setup();

        // Initialize ChromeDriver instance
        WebDriver driver = new ChromeDriver();

        try {
            // Maximize the browser window
            driver.manage().window().maximize();

            // Navigate to the webpage you want to test
            driver.get("https://www.techlistic.com/p/selenium-practice-form.html");

            // Find all checkbox elements on the page using a CSS selector
            List<WebElement> checkboxes = driver.findElements(By.cssSelector("input[type='checkbox']"));

            // Get the total number of checkboxes found on the page
            int totalCheckboxes = checkboxes.size();
            System.out.println("Total number of checkboxes on the page: " + totalCheckboxes);
        } finally {
            // Close the browser after the script completes
            driver.quit();
        }
    }
}

Output:

Given below is the output of the code given above:

checkbox-number-output
checkbox number output

Conclusion

Getting the total number of checkboxes on a webpage using Selenium WebDriver is a straightforward process that involves locating elements using CSS selectors and counting them. This method can be extended to count other types of elements or perform additional validations as needed. By following the steps and using the provided code example, you can easily incorporate this functionality into your automated testing scripts.

This approach ensures that your tests are comprehensive and that the user interface elements are as expected, contributing to a more robust and reliable application.


Next Article
Article Tags :

Similar Reads