Working with Radio Groups
Learn how to find and interact with a radio group using Selenium.
Iterate radio buttons in a radio group
So far, we have been focusing on identifying a specific web control by using one type of
locator, i.e., findElement
. We will now use another type of locator called findElements
.
findElement
is different from findElements
in a sense because findElement
returns one matched control, whereas findElements
return a list of matched controls. This comes in handy when the controls are hard to locate like in the case of a radio group. An example of iterating through a radio group is given below:
driver.findElements(By.name("gender")).then(function(radios){
assert.equal(2, radios.length);
radios.forEach(function(radio){
radio.getAttribute("Value").then(function(radio_value) {
if (radio_value == "female") {
radio.click();
}
});
});
});
Get hands-on with 1400+ tech skills courses.