
How to handle radio button in selenium
In this tutorial we will see how to handle radio button in selenium.
When you try to work on radio button first you need to understand you can opt only one option. If you have two options, you can select only one.
Let’s understand how radio button looks like in HTML. Check below snapshot.

When html page is designed with type as “radio” then radio button will be created it will be reflected as one option to select radio button in a web page. Please look below snapshot.

In selenium webdriver you can handle radio button using click method. Let’s see step wise
Step 1: Inspect the element and find the unique element (Xpath).
Step 2: Find the element and use click method.
Step 3: Validate the whether you have clicked or not using automation.
Step 1: Inspect the element and find the unique element (Xpath).
Xpath: //input[@value=’male’]
After finding Xpath you need to make sure 3 things whether you have written valid Xpath, Element is highlighted and having unique (1 of 1 node in html). Please look below screenshot.

Step 2: Find the element and use click method.
1 2 3 4 5 6 |
//Find element and assign in webelement WebElement maleRadioButton=driver.findElement(By.xpath("//input[@value='male']")); //clicking the element maleRadioButton.click(); |
In above expression click is method to handle radio button using selenium webdriver.
1 |
Step 3: Validate the whether you have clicked or not using automation.
This is main thing we need to consider for every interview or real time as we need to cross check whether we have clicked or not? In Selenium Webdriver there are three methods by using these methods we can validate.
Validating Methods:

All above methods will return Boolean. Either true or false if it returns ‘true’ we can say that element is clicked else not clicked.
Please check below code snippet for validation part:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
//validating radio button is clicked or not? boolean status = maleRadioButton.isSelected(); //confirming what is there in status for learning purpose otherwise no need System.out.println("Checking boolean status:"+status); //checking condition is true or not. if(status) { System.out.println("Male radio button is selected sucessfully at gender."); } else { System.out.println("Male radio button is not clicked at gender."); } |
Based on Boolean status and if condition we can validate easily. Please practice once on this automation practice page click here where you have radio button.
Please check entire program on how to handle radio button in selenium webdriver below,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import org.testng.annotations.Test; public class HandlingRadioButton { @Test public void HandlingRadioButton() { //Initialize reference to driver WebDriver driver; //Setting the properties for chromedriver System.setProperty("webdriver.chrome.driver","./drivers/chromedriver.exe"); //Initialize the chromedriver for webdriver reference driver=new ChromeDriver(); //Launching application driver.get("https://automationexplore.com/selenium-automation-practice-page/"); //Setting implicit wait to load all web elements in application driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS); //Maximize the window driver.manage().window().maximize(); //Find element and assign in webelement WebElement maleRadioButton=driver.findElement(By.xpath("//input[@value='male']")); //clicking the element maleRadioButton.click(); //validating radio button is clicked or not? boolean status = maleRadioButton.isSelected(); //confirming what is there in status for learning purpose otherwise no need System.out.println("Checking boolean status:"+status); //checking condition is true or not. if(status) { System.out.println("Male radio button is selected sucessfully at gender."); } else { System.out.println("Male radio button is not clicked at gender."); } } } |
Output:

Note: To run above program you need to download browser driver as I have downloaded chrome. You can download and work based on your desired web browser driver or you can click here to download from selenium website.
Nice information
Author is superb
Got to know all the required information.. Nice blog
Thank you…….Sir
Useful information…….
Super
Very use full information…. Can you please tell me how to select a radio element if all the attribute of that element are same like if value is also male for both radio button
Thanks
Thanks rawoof. if both elements having same attributes then you can create xpath with index. example:(//input[@value=’male’])[1] for male and (//input[@value=’male’])[2] for female.Hope you got the answer