Articles → SELENIUM → Handling Radio Buttons In Selenium

Handling Radio Buttons In Selenium






Methods For Handling Radio Buttons




  1. click() – Method used to check or uncheck the radio button.
  2. 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>




Picture showing the different types of radio buttons on the web page
Click to Enlarge



  1. Checked the first radio button.
  2. Check if the radio button is checked or not.
  3. Check the ‘Male’ option in second radio button list
  4. 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());
	}
}




Picture showing how radio buttons are handled in selenium
Click to Enlarge


Posted By  -  Karan Gupta
 
Posted On  -  Saturday, June 17, 2017

Query/Feedback


Your Email Id
 
Subject
 
Query/FeedbackCharacters remaining 250