×

Selenium WebDriver- Radio button handling

Selenium WebDriver- Radio button handling

In this tutorial, we will be learning about handling the radio button in selenium WebDriver.

Let us take one example to give you a better understanding of Radio button handling, and we will create a sample test script which is as follows;

For our testing purpose, we are using the Facebook home page to perform Radio button handling in selenium WebDriver.

In this test case, we will automate the following test scenarios:

Steps Action Method Used Input Excepted Result
1. Open the Google Chrome Browser. System.setProperty()   The Google chrome browser must be opened.
2. Navigate to the website. get() https://www.facebook.com/ The home page window must be displayed.
3. Identify the female radio button, and Select the female radio button on the Facebook home page. click()   A female radio button should be identified and selected.
4. Closing the browser window. close()   The browser window should be closed.

Open the Eclipse IDE and the existing test suite new_test, which we have created in WebDriver Installation section of the WebDriver tutorial.

Then, right-click on the src folder and create a new Class File from New ? Class.

Eclipse IDE and the existing test
  • Give the Class name as Radio_button and click on the Finish button.
Class name as Radio_button

Step1:

To launch the Google Chrome browser, we need to download the ChromeDriver.exe file and set the system property to the path of ChromeDriver.exe file.

Here is the code for setting the system property of the Google chrome Browser:

System.setProperty("webdriver.chrome.driver","C:\Users\JTP\Downloads\chromedriver_win32 (1)\chromedriver.exe");
// create driver object for CHROME browser
WebDriver driver=new ChromeDriver(); 

Step2:

Now, we will get to the desired URL:

//navigate to the website.
driver.get("https://www.facebook.com"); 

Step3:

To identify the radio button first, right-click on the female radio button and select the Inspect element field which is as shown below,

right-click on the female radio button

The developer tool window will be launched, with all the specific codes used in the development of the particular radio button and click on the chropath and copy the value of relative XPath which is as shown below,

 click on the chropath and copy the value of relative XPath

Here the sample code,

//identify and click on the radio button
 try {
 driver.findElement(By.xpath("//label[contains(text(),'Female')]")).click(); 
 System.out.println("radio button is clicked");
 Thread.sleep(2000);
 } 
 catch(Exception e)
 {
 System.out.println(e);
 } 

Step4:                                                                                  

Finally, we close the browser window.

 //close the browser
driver.close();

After completing all the given steps, our final text script will look like this:

package testpackage;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class Radio_button {
public static void main(String[] args) throws InterruptedException{ System.setProperty("webdriver.chrome.driver","C:\Users\JTP\Downloads\chromedriver_win32 (1)\chromedriver.exe");
// Create driver object for CHROME browser 
WebDriver driver=new ChromeDriver();
//maximize the window size
driver.manage().window().maximize(); 
driver.manage().timeouts().pageLoadTimeout(20,TimeUnit.SECONDS);
//delect all the cookies
driver.manage().deleteAllCookies();
//navigate to the url
driver.get("https://www.facebook.com/"); 
//identify and click on the radio button
try {
driver.findElement(By.xpath("//label[contains(text(),'Female')]")).click(); 
System.out.println("radio button is clicked");
Thread.sleep(2000);
} 
catch(Exception e) 
{
System.out.println(e);
}
//Close the  browser  
driver.close();  
}
}
  • To run the test script in Eclipse, right-click on the window and then click Run as ? Java application and the test script will be launched in the chrome browser, and automate all the test scenarios.
our final text script will look like

Related Topics

Selenium Tool Suite

The selenium tool suite is a combination of multiple software tools, and the entire tools having a different approach to support automation testing. Selenium test tool suites are as follows- Selenium...

2 minutes read.

Selenium WebDriver-Alert Popup

Selenium WebDriver-Alert Popup  An alert popup is one of the small message boxes, which used to give some information to the user that displays on the browser. The Alert popup will be...

6 minutes read.

Selenium Tutorial

What is Selenium? Selenium is a functional testing tool and compatible with the non-functional testing tools also. Selenium is free and does not require any licensing. Functional tests script executed by...

6 minutes read.

Selenium WebDriver – Window handling

Selenium WebDriver – Window handling In this tutorial, we will understand how we can handle the window, multiple windows, and pop-ups in selenium WebDriver. We have different types of window, and pop-ups...

7 minutes read.

CSS Selector: Class in selenium IDE

CSS Selector: Class Identify a web element with the help of Class as a CSS Selector is like using id attribute. The only difference between Class and id is in their...

2 minutes read.

CSS Selector: Inner text in Selenium IDE

CSS Selector: Inner text The inner text is used to identify and create a CSS Selector using a string pattern of the HTML tag which is displayed on the web page. Syntax...

1 minute read.

Selenium-IDE Login test

Login test for Selenium-IDE Now we are learning, how to create a Login/sign-up Test case in Selenium IDE. For our testing purpose, we are going to test the login page provided by...

2 minutes read.

Selenium WebDriver- Microsoft Edge Browser

Selenium WebDriver- Microsoft Edge Browser: In this tutorial, we will learn how to launch the Microsoft Edge browser in the Selenium WebDriver. Before we start automating our test script with Microsoft Edge...

4 minutes read.

Selenium WebDriver-CSS Selector: Substring Matching

CSS Selector: Substring Matching Selenium WebDriver In this tutorial, we will learn how to identify the web element using the substring method. A Sub-string contains three methods for identifying the web element...

9 minutes read.

Selenium IDE Commands

Selenium –IDE Commands (SELENESE): Selenese commands are the set of commands used in Selenium IDE. Selenese command has two parameters target and value. Selenese command tests the existence of UI elements based on their HTML tags,...

3 minutes read.

Selenium WebDriver-Locators

Selenium WebDriver-Locators The WebDriver uses the same set of locating strategies like selenium -IDE for specifying the location of the web element. Each locating strategy has its command in Java to locate...

1 minute read.

WebDriver-Tag Name Locator

Selenium WebDriver-Tag Name Locator The tag name locator is used along with the findElements() method to identify multiple similar elements in the UI. It is used to locate the web element with...

3 minutes read.

Selenium WebDriver installation

Installation Steps of Selenium WebDriver In this tutorial, we will see the process of selenium webdriver installation. Selenium WebDriver installation process is done in four Stages: Java Installation (version 8 or higher version) Eclipse...

4 minutes read.

Selenium-IDE Installation in Mozilla Firefox

A beginners guides to Selenium-IDE Installation Since Selenium IDE is designed only for Firefox and Chrome plug-in, we are assuming that you have already installed the Mozilla Firefox browser in your...

1 minute read.

Selenium Testing

Selenium is a functional testing tool and compatible with non–functional testing tool. Selenium is free and not required any licensing. There are specified types of testing that can be automated using Selenium:-   Functional Testing: Checking every component...

3 minutes read.

Difference between Selenium WebDriver and Selenium RC

Difference between Selenium WebDriver, and Selenium RC: Selenium WebDriver and selenium RC (Remote Control) both are different in any manners which we see later in this tutorial, let us see the...

3 minutes read.

XPath Axes Selenium WebDriver

XPath Axes XPath axes are those axes that are used to search for the multiple nodes in the XML document from the current node context. These methods are mainly used when the...

14 minutes read.

Selenium WebDriver - Google Chrome Browser

Selenium WebDriver - Google Chrome Browser In this tutorial, we will learn how to run our Selenium Test Scripts in the Google Chrome Browser. To implement the WebDriver protocol with the help...

3 minutes read.

Selenium WebDriver- Checkbox handling

Selenium WebDriver- Checkbox handling In this tutorial, we will be learning about handling the checkbox in selenium WebDriver. Let us take one example to give you a better understanding of Checkbox handling,...

4 minutes read.

Selenium Assertion

Selenium Assertion: In this section, we will learn about the Assertion in Selenium WebDriver. An Assertion is a feature available in TestNG, which is used to verify the expected result of...

7 minutes read.