Handling radio buttons in selenium

Radio buttons usually belong to a group So all the radio buttons from the group have the same name And only one radio can be selected from a group and the radio button that is selected has a checked attribute =true and unchecked will have checked attribute=null.In the below example we use a webelements list to find all the elements that have the name group1.We print how many radio buttons are there in the group and we print which all are unchecked and which "one" of them is checked.

List<WebElement> radios = driver.findElements(By.xpath("//input[@name='group1']"));
System.out.println("Total radio buttons -> "+ radios.size());
for(int i=0;i<radios.size();i++)
{ System.out.println(radios.get(i).getAttribute("checked"));
}