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