Selenium WebDriver Handling Radio Buttons Using Java
Last Updated :
11 Mar, 2024
A platform-independent language that is used to build various applications is known as Java. Java can also be used to automate the web drivers. There are various automation tools available, out of which Selenium is the most common one. We can automate the opening of the website, clicking of push button, and enabling radio buttons or checkboxes. In this article, we will discuss how we can handle the radio buttons using a selenium web driver in Java.
Selenium WebDriver Handling Radio Buttons Using Java
For handling the radio buttons using the Selenium web driver in Java, we need to follow the respective steps:
Step 1: Setting up the Selenium in Java
Set up Selenium in Java by downloading the latest JARs from the official website, then add them to your project's build path using "Configure Build Path" and selecting "Add External JARs" in Eclipse or a similar IDE.
Step 2: Installing the necessary modules
We need to install the By WebDriver and ChromeDriver modules.
- By: The By module is used to locate elements within the document and perform a specific action on them.
- WebDriver: The WebDriver is a remote control interface that is used to create robust browser-based regression test suites.
- ChromeDriver: An open-source tool that is used for automated testing of web apps is called ChromeDriver.
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
Step 3: Initiating the web driver and navigate to the webpage
Now, we have defined the path where the Chrome driver is present and then initiated the Chrome driver by using the ChromeDriver module we imported. As Chrome is the most common browser used in daily life, thus we have used the ChromeDriver. You can use any other browser driver of your choice too. Here, we have opened the GeeksforGeeks website (link) for handling the radio buttons.
- get(): The get() function is used to open the URL in the browser. It will navigate to the webpage in which we want to handle the radio buttons using the get() function.
Java
//Import selenium libraries
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.By;
public class selenium4 {
public static void main(String[] args) {
// State the chromedriver URL
System.setProperty("webdriver.chrome.driver","C:\\Users\\Vinayak Rai\\Downloads\\chromedriver-win64\\chromedriver-win64\\chromedriver.exe");
// Define and initiate the chrome driver
WebDriver driver = new ChromeDriver();
// Open the Geeks For Geeks website
driver.get("https://www.geeksforgeeks.org/explore?page=1&sortBy=submissions&itm_source=geeksforgeeks&itm_medium=main_header&itm_campaign=practice_header");
}
}
Step 4: Finding an element
Further, we find the element using findElement, By.xpath, and contains functions available in Selenium.
- findElement: The function that is used to return the first matching occurrence on the webpage is called the findElement function.
- By.xpath: The crucial language that is used in navigation for HTML documents is known as Xpath, while the function that is used to locate the element based on Xpath is known as the By.xpath function.
- contains(): When we need to locate an element that contains a certain substring and not the complete string, we use the contains() function.
Java
//Import selenium libraries
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.By;
public class selenium4 {
public static void main(String[] args) {
// State the chromedriver URL
System.setProperty("webdriver.chrome.driver","C:\\Users\\Vinayak Rai\\Downloads\\chromedriver-win64\\chromedriver-win64\\chromedriver.exe");
// Define and initiate the chrome driver
WebDriver driver = new ChromeDriver();
// Open the Geeks For Geeks website
driver.get("https://www.geeksforgeeks.org/explore?page=1&sortBy=submissions&itm_source=geeksforgeeks&itm_medium=main_header&itm_campaign=practice_header");
// Maximize the screen
driver.manage().window().maximize();
// Click on the pop-up
driver.findElement(By.xpath("/html/body/div[1]/div/div[3]/div[2]/div/div[2]/div/div/div/button")).click();
// Finding and clicking the radio button 'SDE Sheet'
WebElement boolean_value = driver.findElement(By.xpath("// *[contains(text(),'SDE Sheet')]"));
}
}
Output:

Step 5: Click on an element
Finally, we will click an element, i.e., the radio button name 'SDE Sheet' found on the website using the click function.
- click(): The function which is used for interacting with the clickable elements, like buttons, link, etc is called the click function.
Java
//Import selenium libraries
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.By;
public class selenium4 {
public static void main(String[] args) {
// State the chromedriver URL
System.setProperty("webdriver.chrome.driver","C:\\Users\\Vinayak Rai\\Downloads\\chromedriver-win64\\chromedriver-win64\\chromedriver.exe");
// Define and initiate the chrome driver
WebDriver driver = new ChromeDriver();
// Open the Geeks For Geeks website
driver.get("https://www.geeksforgeeks.org/explore?page=1&sortBy=submissions&itm_source=geeksforgeeks&itm_medium=main_header&itm_campaign=practice_header");
// Maximize the screen
driver.manage().window().maximize();
// Click on the pop-up
driver.findElement(By.xpath("/html/body/div[1]/div/div[3]/div[2]/div/div[2]/div/div/div/button")).click();
// Finding and clicking the radio button 'SDE Sheet'
WebElement boolean_value = driver.findElement(By.xpath("// *[contains(text(),'SDE Sheet')]"));
// Click on an element
boolean_value.click();
}
}
Output:

Conclusion
In conclusion, handling radio buttons using Selenium WebDriver in Java involves setting up Selenium, installing necessary modules like By WebDriver and ChromeDriver, initializing the WebDriver, navigating to the webpage, finding the radio button element, and clicking on it using Selenium functions. This allows the automation of interactions with radio buttons on web pages, enhancing the efficiency of web testing and interaction processes.
Similar Reads
Software Testing Tutorial Software testing is an important part of the software development lifecycle that involves verifying and validating whether a software application works as expected. It ensures reliable, correct, secure, and high-performing software across web, mobile applications, cloud, and CI/CD pipelines in DevOp
10 min read
What is Software Testing? Software testing is an important process in the Software Development Lifecycle(SDLC). It involves verifying and validating that a Software Application is free of bugs, meets the technical requirements set by its Design and Development, and satisfies user requirements efficiently and effectively.Here
11 min read
Principles of Software testing - Software Testing Software testing is an important aspect of software development, ensuring that applications function correctly and meet user expectations. From test planning to execution, analysis and understanding these principles help testers in creating a more structured and focused approach to software testing,
3 min read
Software Development Life Cycle (SDLC) Software Development Life Cycle (SDLC) is a structured process that is used to design, develop, and test high-quality software. SDLC, or software development life cycle, is a methodology that defines the entire procedure of software development step-by-step. The goal of the SDLC life cycle model is
8 min read
Software Testing Life Cycle (STLC) The Software Testing Life Cycle (STLC) is a process that verifies whether the Software Quality meets the expectations or not. STLC is an important process that provides a simple approach to testing through the step-by-step process, which we are discussing here. Software Testing Life Cycle (STLC) is
7 min read
Types of Software Testing Software testing is a important aspect of software development life-cycle that ensures a product works correctly, meets user expectations, and is free of bugs. There are different types of software testing, each designed to validate specific aspects of an application, such as functionality, performa
15+ min read
Levels of Software Testing Software Testing is an important part of the Software Development Life Cycle which is help to verify the product is working as expected or not. In SDLC, we used different levels of testing to find bugs and errors. Here we are learning those Levels of Testing in detail.Table of ContentWhat Are the Le
4 min read
Test Maturity Model - Software Testing The Test Maturity Model (TMM) in software testing is a framework for assessing the software testing process to improve it. It is based on the Capability Maturity Model(CMM). It was first produced by the Illinois Institute of Technology to assess the maturity of the test processes and to provide targ
8 min read
SDLC MODELS
TYPES OF TESTING