Articles → SELENIUM → Handling Radio Buttons In Selenium
Handling Radio Buttons In Selenium
Methods For Handling Radio Buttons
- click() – Method used to check or uncheck the radio button.
- isSelected() – Method used to check if the radio button is selected or not.
Example
<!DOCTYPE html>
<html lang="en"
xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<title></title>
</head>
<body>
Single Radio button
<input type="radio" id="rdSingle" />
<br />
<br />
<br />
<br />
Select the Gender
<br />
Male
<input type="radio" id="M" name="gender" />
<br />
Female
<input type="radio" id="F" name="gender" />
<br />
<br />
<br />
<br />
Select the Gender
<br />
Male
<input type="radio" id="M" name="gender1" checked />
<br />
Female
<input type="radio" id="F" name="gender1" />
<br />
</body></html>
Click to Enlarge
- Checked the first radio button.
- Check if the radio button is checked or not.
- Check the ‘Male’ option in second radio button list
- Check if the option ‘Male’ is selected in third radio button list or not.
package com.firstapp;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
public class TestClass {
public static void main(String[] args) {
WebDriver driver;
String service = "IEDriverServer path";
System.setProperty("webdriver.ie.driver", service);
driver = new InternetExplorerDriver();
driver.get("http://localhost:18275/Page1.html");
driver.findElement(By.id("rdSingle")).click();
System.out.println(driver.findElement(By.id("rdSingle")).isSelected());
driver.findElement(By.name("gender")).findElement(By.id("M")).click();
System.out.println(driver.findElement(By.name("gender1")).findElement(By.id("M")).isSelected());
}
}
Click to Enlarge