Automating radio buttons and radio groups
The Selenium WebDriver supports radio buttons and radio group elements using the WebElement
interface. 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 that will have radio buttons and the radio group controls. We will perform select and deselect operations, the following code shows an example:
@Test public void testRadioButton() { // Get the Radio Button as WebElement using it's value attribute WebElement petrol = driver.findElement(By .xpath("//input[@value='Petrol']")); // Check if its already selected? otherwise select the Radio Button // by calling click() method if (!petrol.isSelected()) { petrol.click(); } // Verify Radio Button is selected assertTrue(petrol.isSelected...