Automating radio buttons and radio groups
Selenium WebDriver supports Radio Button and Radio Group controls using the WebElement
class. We can select and deselect the radio buttons using the click()
method of the WebElement
class and check whether a radio button is selected or deselected using the isSelected()
method.
In this recipe, we will see how to work with the Radio Button and Radio Group controls.
How to do it...
Let's create a test which gets Radio Button and Radio Group controls. We will perform select and deselect operations.
@Test public void testRadioButton() { //Get the Radiobutton as WebElement using it's value attribute WebElement petrol = driver.findElement(By.xpath("//input[@value='Petrol']")); //Check if its already selected? otherwise select the Radiobutton //by calling click() method if (!petrol.isSelected()) petrol.click(); //Verify Radiobutton is selected assertTrue(petrol.isSelected()); //We can also get all the Radiobuttons from a Radio...