Open In App

How to get the total number of radio buttons on a page using Selenium?

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

When automating web testing with Selenium, one common task is to count the total number of radio buttons on a page. Radio buttons are crucial for selecting one option from a set of choices, and ensuring they are correctly displayed can be important for validating form functionality. In this guide, we'll walk through a simple yet effective method to retrieve the total number of radio buttons on a web page using Selenium WebDriver.

  • We can get the total number of buttons on a page using Selenium webdriver using the find_elements method.
  • While working on any radio buttons, we will always find an attribute type in the html code and its value should be radio.
  • This characteristic is only applicable to radio buttons on that particular page and to no other type of UI elements like edit box, link, and so on.
  • To retrieve all the elements with attribute type = 'radio', we will use the find_elements-by_xpath() method.

Example of How to get the total number of radio buttons on a page

POM.XML

XML
<dependencies>
    <!-- Selenium WebDriver dependency -->
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version>4.23.1</version> <!-- Use the latest version available -->
    </dependency>

    <!-- WebDriverManager dependency -->
    <dependency>
        <groupId>io.github.bonigarcia</groupId>
        <artifactId>webdrivermanager</artifactId>
        <version>5.7.1</version> <!-- Use the latest version available -->
    </dependency>
</dependencies>


RadioButtonCountArtOfTesting.java

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 RadioButtonCountArtOfTesting {
    public static void main(String[] args) {
        // Set up ChromeDriver using WebDriverManager
        WebDriverManager.chromedriver().setup();
        WebDriver driver = new ChromeDriver();

        try {
            // Open the web page
            driver.get("https://artoftesting.com/samplesiteforselenium");

            // Find all radio buttons on the page
            List<WebElement> radioButtons = driver.findElements(By.cssSelector("input[type='radio']"));

            // Get the count of radio buttons
            int totalRadioButtons = radioButtons.size();
            System.out.println("Total number of radio buttons: " + totalRadioButtons);
        } finally {
            // Close the WebDriver
            driver.quit();
        }
    }
}

Output:

radio-buttons-using-Selenium-output
radio buttons using Selenium output

Explanation of How to get the total number of radio buttons on a page

1. Imports

  • By, WebDriver, WebElement, ChromeDriver from Selenium.
  • WebDriverManager for managing WebDriver binaries.
  • List for handling collections of elements.

2. Main Method

  • Initializes WebDriver using WebDriverManager.
  • Creates a new ChromeDriver instance.

3. Open Web Page

  • Navigates to "https://artoftesting.com/samplesiteforselenium".

4. Find All Radio Buttons

  • Locates all <input> elements of type radio using a CSS selector.

5. Get and Print Radio Button Count

  • Retrieves the count of radio buttons and prints it to the console.

6. Close WebDriver

  • Ensures the browser is closed using driver.quit() in the finally block.

7. Output

  • Right click on the program and after that select as Run As > Java Application.

Conclusion

Counting radio buttons using Selenium is a straightforward process that involves locating all radio button elements on a page and then determining their count. By leveraging Selenium's powerful features, you can easily automate this task to ensure your web application's forms are functioning as expected. This method not only helps in validating the presence of radio buttons but also aids in ensuring the correctness of dynamic web content.


Article Tags :

Similar Reads