TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

Selenium WebDriver Handling Radio Buttons

Selenium WebDriver Handling Radio Buttons with Introduction, features, selenium basic terminology, what is selenium, selenium limitations, selenium vs qtp, tool suite, selenium ide, ide-installation, ide-features, ide-first test case, ide-commands, ide-creating test cases manually, ide-login test etc.

<< Back to SELENIUM

Handling Radio buttons

In this section, you will learn how to handle radio buttons in selenium web driver.

Following are the steps to handle the radio buttons:

Step 1: Invoke the Google Chrome browser.

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 navigate to the website in which we need to handle the radio buttons.

I created the html file which contains the radio buttons. The code is given below:





Mango
Mango
Mango

Ladyfinger
Potato
Tomato

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");
        }

}

The output of the above code:

Handling Radio buttons

Step 3: Select the option Banana. We will locate the Banana radio button by inspecting its HTML codes.

There are two ways of handling the radio buttons:

  • By using the customized path:

The code shown below handles the radio button by using the customized 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='Banana']")).click();
        }

}

In the above, we use custom Xpath. Radio buttons contain a unique attribute, i.e., value, so we use the value attribute to handle the radio button.

Output

Handling Radio buttons
  • By handling the radio buttons dynamically.
    • We will first calculate the number of radio buttons. The following is the line of code which calculates the number of radio buttons.

      int a = driver.findElements(By.xpath("//input [@name='group1']")).size();

      The above line of code calculates the number of radio buttons whose name is group1.
    • Now, we will handle the radio buttons by using the index of a particular radio button.

      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();
        }
    }}

In the above code, we use 'for' loop. Inside the 'for' loop, we find the third radio button of group1 by using the get(2) method.

Output

Handling Radio buttons



Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf