• Selenium Video Tutorials

Selenium WebDriver - Radio Buttons



Selenium Webdriver can be used to handle radio buttons on a web page. In HTML terminology, every radio button is identified by the tagname called input. Also, each radio button on a web page will always have an attribute called type with its value as radio.

Identification of Radio Buttons in HTML

Launch a browser, say Chrome, and right click on the web page, and then click on the Inspect button. For the identifying the radio button on the page, click on the left upward arrow as highlighted below.

Selenium Button Radio 1

Once we had clicked and pointed the arrow to the radio button (highlighted in the below image), its HTML code was visible, reflecting the input tagname and value of type attribute as radio.

Selenium Button Radio 2

Click and Check Radio Button

Let us take an example of the above page, where we would click one of the radio buttons with the help of the click() method. Then we would verify if the radio button is clicked using the isSelected() method. This method returns a boolean value (true or false). If a radio button is selected, isSelected() would return true, else false.

Example

package org.example;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.concurrent.TimeUnit;

public class HandlingRadioButton {
   public static void main(String[] args) throws InterruptedException {

      // Initiate the Webdriver
      WebDriver driver = new ChromeDriver();

      // adding implicit wait of 15 secs
      driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);

      // Opening the webpage where we will identify radio button
      driver.get("https://www.tutorialspoint.com/selenium/practice/selenium_automation_practice.php");

      // identify radio button then click
      WebElement radiobtn = driver.findElement(By.xpath("//*[@id='gender']"));
      radiobtn.click();

      // verify if radio button is selected
      boolean result = radiobtn.isSelected();
      System.out.println("Checking if a radio button is selected: " + result);

      // Closing browser
      driver.quit();
   }
}

Output

Checking if a radio button is selected: true

Process finished with exit code 0

In the above example, we had first clicked on a radio button, and then verified if the radio button was clicked in the console with the message - Checking if a radio button is selected: true.

Finally, the message Process finished with exit code 0 was received, signifying successful execution of the code.

Count Radio Buttons

Let us take an example of the below page, where we would count the total number of radio buttons. In this example, the total number of radio buttons should be 3.

Selenium Radio 3

Example

package org.example;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.List;
import java.util.concurrent.TimeUnit;

public class HandlingRadioButton {
   public static void main(String[] args) throws InterruptedException {

      // Initiate the Webdriver
      WebDriver driver = new ChromeDriver();

      // adding implicit wait of 15 secs
      driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);

      // Opening the webpage where we will identify radio button
      driver.get("https://www.tutorialspoint.com/selenium/practice/selenium_automation_practice.php");

      // Retrieve all radio buttons using locator and storing in List
      List<WebElement> totalRadioBtns = driver.findElements(By.xpath("//input[@type='radio']"));

      // count number of radio buttons
      int count = totalRadioBtns.size();
      System.out.println("Count the radio buttons: " + count);

      // Closing browser
      driver.quit();
   }
}

Output

Count the radio buttons: 3

In the above example, we had counted the total number of radio buttons on a web page, and received the messages in the console - Count the radio buttons: 3.

Validate Radio Buttons

Let us take an example of the above web page, where we would perform some validations on the radio buttons. First, we would check if the radio button is enabled/disabled using the isEnabled() method. Also, we would verify if it is displayed and selected/unselected using the isDisplayed() and isSelected() methods respectively.

These methods return a boolean value(true or false). If a radio button is selected, enabled and displayed it would return true, else false.

Example

package org.example;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.concurrent.TimeUnit;

public class ValidateRadioButton {
   public static void main(String[] args) throws InterruptedException {
   
      // Initiate the Webdriver
      WebDriver driver = new ChromeDriver();
      
      // adding implicit wait of 15 secs
      driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
      
      // Opening the webpage where we will click radio button
      driver.get("https://www.tutorialspoint.com/selenium/selenium_automation_practice.htm");
      
      // identify radio button then click
      WebElement radiobtn = driver.findElement
         (By.xpath("//*[@id='practiceForm']/div[3]/div/div/div[2]/input"));
      radiobtn.click();
      
      // verify if radio button is selected
      boolean result = radiobtn.isSelected();
      System.out.println("Checking if a radio button is selected: " + result);
      
      // verify if radio button is displayed
      boolean result1 = radiobtn.isDisplayed();
      System.out.println("Checking if a radio button is displayed: " + result1);
      
      // verify if radio button is enabled
      boolean result2 = radiobtn.isEnabled();
      System.out.println("Checking if a radio button is enabled: " + result2);
      
      // identify another radio button
      WebElement radiobtn1 = driver.findElement(By.xpath("//*[@id='gender']"));
      
      // verify if radio button is not selected
      boolean result3 = radiobtn1.isSelected();
      System.out.println("Checking if the other radio button is unselected: " + result3);
             
      // Closing browser
      driver.quit();
   }
}

Output

Checking if a radio button is selected: true
Checking if a radio button is displayed: true
Checking if a radio button is enabled: true
Checking if the other radio button is unselected: false

In the above example, we had validated if a radio button was displayed, enabled, and selected, and received the following messages in the console - Checking if a radio button is selected: true, Checking if a radio button is displayed: true, Checking if a radio button is enabled: true and Checking if the other radio button is unselected: false.

Conclusion

This concludes our comprehensive take on the tutorial on Selenium Webdriver Radio Buttons. Weve started with describing identification of radio buttons in HTML, and examples to illustrate how to handle radio buttons in Selenium Webdriver. This equips you with in-depth knowledge of the Selenium Webdriver Radio Buttons. It is wise to keep practicing what youve learned and exploring others relevant to Selenium to deepen your understanding and expand your horizons.

Advertisements