Check out my iOS apps here
As an Amazon Associate I earn from qualifying purchases.

How to locate, select & iterate through Radio buttons with Selenium WebDriver & Java

Let’s Interact With Radio Buttons & Radio Buttons Group Using Selenium Web Driver

In the previous tutorials, we have seen how we can click on a WebElement using the click() function, fill textbox with text using the sendKeys() functions. These are the functions you will need most of the time. But some forms have other controls, like radio buttons, and you will need some other functions to interact with them.

In this tutorial, we will learn how to locate & interact with radio buttons with Selenium Web Driver in various ways, as well as iterating through all the radio buttons in a given group.

This tutorial is part of a series about Selenium Web Driver. You can go to the first part in the series by clicking here.. For setting up Selenium & launching the first test, you can refer to the first lesson in the series:- Getting Started With Selenium WebDriver tutorial

Locating & Selecting Radio Buttons

Using what we learned about locating WebElements in this series ☇, you can locate & interact with Radio Buttons just like any WebElement. After that, we could check its status & select it using the various functions of WebElement.

If the radio button we wish to click had a unique value, like an ID, then finding it is straightforward. If you ever needed, you could always use Xpath or CSS selector to find the radio button you want (using its value or some other criterea). Xpath & CSS selector are explained in their respective tutorials. ☇

In our radio buttons test form, we have 4 radio buttons. Each button represents an animal:-

Let’s write a Java code that locates & click on the Penguin radio button. We could simply locate it using its ID (PenguinID). We got that value from the inspector here:-

The following code locates & select it using the click() function:-

package SeleniumSeries;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
public class SeleniumRadioButton {
public static void main(String[] args) {
String GeckoPath = "C:\\Fairy\\geckodriver.exe";
System.setProperty("webdriver.gecko.driver", GeckoPath);
WebDriver driver = new FirefoxDriver();
driver.get("https://tech-fairy.com/selenium-webdriver-paractice-form-with-radio-buttons/");
WebElement radio = driver.findElement(By.id("PenguinID"));
radio.click();
}
}

For simple cases, where you know which radio button you want to click, this solution works just fine. We will learn how to iterate through radio buttons of the same group later in this tutorial.

Radio Button Status

You can determine whether the radio button is selected with the WebElement function isSelected(). It returns true of the button is selected, and false if it was not.

radio.isSelected();



Check If Radio Button Is Enabled

In the same way we could check the selection status of WebElements. You could check whether a radio button is enabled or not. This can be useful to avoid trying to select a radio button we can’t select in the first place. Checking the button status can be done using the WebElement function isEnabled():-

radio.isEnabled();

Iterating And Interacting With Radio Buttons In The Same Group

You can iterate through all the available radio buttons in a given group. Radio buttons of the same group share the same name, although you can’t use that to locate a specific button, it can be used to locate all them.

The following code iterate through all the buttons in the Animal group, which all have the name “Animal”, and tells us which one is currently selected using the .isSelected() function:-

package SeleniumSeries;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
public class IterateThroughRadioButtons {
public static void main(String[] args) {
String GeckoPath = "C:\\Fairy\\geckodriver.exe";
System.setProperty("webdriver.gecko.driver", GeckoPath);
WebDriver driver = new FirefoxDriver();
driver.get("https://tech-fairy.com/selenium-webdriver-paractice-form-with-radio-buttons/");
List AllRadioButtons = driver.findElements(By.name("Animal"));
for(WebElement RadioNutton : AllRadioButtons) {
String SelectedButtonStatus = "(Unselected)";
boolean IsButtonSelected = RadioNutton.isSelected();
if(IsButtonSelected) {
SelectedButtonStatus = "(Selected)";
}
System.out.println("The radio button \"" + RadioNutton.getAttribute("value") + "\" " + SelectedButtonStatus);
}
}
}

Not only iterating through the radio button makes it easy to check on which one is selected, but it also makes it easy for us to check if the button was enabled before we click on it. It’s also useful in case the radio buttons are dynamic & change based on the user’s input.

Assuming the second button is selected by the time we run this code. We will get the following output:-

The radio button "Cat" (Unselected)
The radio button "Penguin" (Selected)
The radio button "Elephant" (Unselected)
The radio button "Dog" (Unselected)

And Finally

Finding & interacting with radio buttons with Selenium Web Driver is the same as any other WebElement. Which makes it easy to work with them if you knew which functions to use for that. Those functions can be applied to other Input & WebElements, like CheckBoxes. ☇

I hope you found this tutorial useful, and see you again in another one.

See Also:-

Leave a Comment

Your email address will not be published. Required fields are marked *