Working with Radio Buttons

Learn how to find and interact with radio buttons using Selenium.

Select a radio button by ID

Most of the time, we can use the ID of the radio button to click it.

driver.findElement(By.id("radio_female")).click();

Select a radio button by name

Another less efficient way of selecting a radio button is to use its name:

driver.findElement(By.xpath("//input[@name='gender' and @value='female']")).click();
driver.sleep(200);
driver.findElement(By.xpath("//input[@name='gender' and @value='male']")).click();

However, note that the radio buttons in the same radio group have the same name assigned to them. To click one radio option from the group, we have to specify its value.

The value (label) of the radio button is not shown next to it. Therefore, to find out the value of a radio button, we have to inspect its HTML source.

Get hands-on with 1400+ tech skills courses.