Articles → SELENIUM → Handling radio buttons in Selenium

Handling radio buttons in Selenium






Methods for handling radio buttons









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













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