How To Select Radio Buttons In Selenium WebDriver?

I want to know the Selenium webdriver code to select radio buttons. Please help.

Methods For Selecting Radio Elements Using Selenium

Selenium includes different ways using which we can select the Radio elements on a Web Page.

The different methods are as below:

  1. By ID attributes
  2. Using Is Selected()
  3. Using Name
  4. Using element Value
  5. By CSS Selector
  6. With XPath

Consider the below image for understanding the examples of each of the selection methods:

selection methods

#1) Using ID Attributes

In this case, we just need to use the ID attribute, regardless of whatever its value is. The ID attribute is used in Selenium for selecting the desired Radio button. ID attributes are different for different elements.

For example:

WebElement radio = driver.findElement(By.id("i-red")); radio.click();

OR

WebElement radio1 = driver.findElement(By.id("i-green")); radio1.click();

OR

WebElement radio2 = driver.findElement (By.id("i-yellow")); radio2.click();

As shown in the above image, there are six Radio buttons i.e. Red, Blue, Green, Yellow, Gray, and Black. For selecting a Radio button from these, we can make use of ID attributes in Selenium for Radio button selection in this way.

The below image highlights the id attributes for the above options.

Image highlighting id Attributes

#2) Using Is Selected()

Use of Is Selected() enables the user to ensure whether a particular Radio button is in checked mode or not. In simple words, the selection of the Radio button is confirmed.

For example:

List radio = driver.findElements(By.name("colour”));
 
//same group of Radio buttons always have same 'Names', 
//hence we need to use findElements method and store the list of Web elements.
 
boolean bval = false;        // create a variable which will have boolean value True/False
bval = radio.get(0).isSelected();    // will return True if button is selected.
 
if(bval = true)
{
radio.get(1).click(); // if the first radio button is selected by default, this will select Second radio button
}
else
{
radio.get(0).click(); // if the first radio button is not selected by default, the first will be selected
}

#3) Using Name

For Selecting Radio buttons using Name, we need to understand one important point i.e. For Radio Buttons in the same group (Radio Group), Name is always the same but their Values are different.

So if any element with the name attribute is found, then it might contain more than one element, thus we need to store the list of WebElements and make use of the findElements method.

List radio = driver.findElements(By.name(“colour”));

This was used in the previous example for Is Selected() as well.

#4) Using Element Value

Radio buttons can be selected with the help of their Values too.

Each button has a unique value. The use of the Values for selecting the Radio buttons can be as shown in the below example.

List RadioButton = driver.findElements(By.name("colour"));
           // selecting the Radio buttons by Name
 
           int Size = RadioButton.size();                // finding the number of Radio buttons
 
           for(int i=0; i < Size; i++)                      // starts the loop from first Radio button to the last one
          { 
        String val = RadioButton.get(i).getAttribute("value");
    // Radio button name stored to the string variable, using 'Value' attribute
 
        if (val.equalsIgnoreCase("Green"))   // equalsIgnoreCase is ignore case(upper/lower)
                 {                 // selecting the Radio button if its value is same as that we are looking for
        RadioButton.get(i).click();
        break;
            }
              }

#5) By CSS Selector

Another way for selecting Radio elements is by CSS Selector using its value.

The below example will explain the same:

WebElement rbutton = driver.findElement(By.cssSelector("input[value=' Yellow']")); rbutton.click();

#6) Using XPATH

Selecting a Radio element using XPATH is one of the ways for Radio element selection in Selenium that helps to select the exact element you wish to select.

Look at the below example for selecting the Radio button using XPATH.

WebElement radio_b1 = d.findElement(By.*xpath*("//input[@id='i-red']")); radio_b1.click();’

OR

WebElement radio_b2 = d.findElement(By.*xpath*("//input[@id='i-green’]")); radio_b2.click();

OR

WebElement radio_b3 = d.findElement(By.*xpath*("//input[@id=’i-gray’]")); radio_b3.click();

Answer source: https://www.softwaretestinghelp.com/radio-buttons-in-selenium/