Home >>Selenium Tutorial >Handling Radio buttons

Handling Radio buttons

Handling Radio buttons

In this section, you'll learn how to manage selenium web driver radio buttons.

The procedures to manage the radio buttons are then followed:

Step 1: Invoke a browser running Google Chrome.

The code to invoke a Google chrome browser is given below:

package mypack;  
import org.openqa.selenium.WebDriver;  
import org.openqa.selenium.chrome.ChromeDriver;  
public class Class1   
{  
    public static void main(String[] args)   
    {  
          
        System.setProperty("webdriver.chrome.driver", "C:\\work\\chromedriver.exe");  
        WebDriver driver = new ChromeDriver();  
          
    }  
  
}  

Step 2 : The second step is to move to the web site where the radio buttons must be managed.

I built an html file that contains the buttons buttons. The specification is set out below:

<html>  
<head>  
</head>  
<body>  
<input type="radio" name="group1" value="html">HTML<br>  
<input type="radio" name="group1" value="css">CSS<br>  
<input type="radio" name="group1" value="js">JS<br>  
<hr>  
<input type="radio" name="group2" value="php">PHP<br>  
<input type="radio" name="group2" value="jQ">jQuery<br>  
<input type="radio" name="group2" value="boots">Bootstrap<br>  
</body>  </html>  

The code for navigating to the above html file is given below:

package mypack;  
import org.openqa.selenium.WebDriver;  
import org.openqa.selenium.chrome.ChromeDriver;  
  
public class Class1 {  
    public static void main(String[] args) {  
          
        System.setProperty("webdriver.chrome.driver", "C:\\work\\chromedriver.exe");  
        WebDriver driver = new ChromeDriver();  
        driver.get("file:///C:/Users/admin/Desktop/radio.html");  
        }  
  
}  

Step 3: Choose Banana as option. Through examining their HTML codes, we can find the radio button in JS.

The radio buttons are treated in two ways:

  • Use the Custom Path:

The code illustrated below handles the radio button using the custom path.

package mypack;  
import org.openqa.selenium.By;  
import org.openqa.selenium.WebDriver;  
import org.openqa.selenium.chrome.ChromeDriver;  
  
public class Class1 {  
    public static void main(String[] args) {  
          
        System.setProperty("webdriver.chrome.driver", "C:\\work\\chromedriver.exe");  
        WebDriver driver = new ChromeDriver();  
        driver.get("file:///C:/Users/admin/Desktop/radio.html");  
        driver.findElement(By.xpath("//input[@value='JS']")).click();  
        }  
  
}  

We use custom-made Xpath in the above. Radio buttons have a special attribute, i.e., meaning, and we use the meaning attribute to manage the button on the radio.

Now we can use the index of a particular radio button to manage the radio buttons.

  • And creative handling of the radio buttons.
  • We'll first calculate the number of buttons on the radio. The line of code that calculates the number of radio buttons is as follows.
  • int a = driver.findElements(By.xpath("//input [@name='group1']")).size();
  • The code line above measures the number of radio buttons with group1 as their name.
  • driver.findElements(By.xpath("//input[@name='group1']")).get(2).click();

Source code

package mypack;  
import org.openqa.selenium.By;  
import org.openqa.selenium.WebDriver;  
import org.openqa.selenium.chrome.ChromeDriver;  
  
public class Class1 {  
    public static void main(String[] args) {  
          
        System.setProperty("webdriver.chrome.driver", "C:\\work\\chromedriver.exe");  
        WebDriver driver = new ChromeDriver();  
        driver.get("file:///C:/Users/admin/Desktop/radio.html");  
        int a = driver.findElements(By.xpath("//input [@name='group1']")).size();  
        System.out.println(a);  
        for(int i=1;i<=a;i++)  
        {  
            driver.findElements(By.xpath("//input[@name='group1']")).get(2).click();  
        }  
   }}  

We use 'for' loop in the function above. In the 'for' loop, we locate group1 's third radio button by using the form get(2).


No Sidebar ads