What is a Radio Button?

A Radio Button is a type of UI element that allows users to select only one option from a group of choices. Unlike checkboxes, where multiple selections are possible, radio buttons work in groupsβ€”selecting one option automatically deselects the others.


How to select a Radio Button using Selenium WebDriver?

To select a radio button in Selenium WebDriver:
βœ… Locate the radio button using an appropriate locator (ID, Name, XPath, CSS Selector, etc.).
βœ… Use the .click() method to select it.

Example:

WebElement radioBtn = driver.findElement(By.id("radioButtonId"));
radioBtn.click();  // Clicks the radio button to select it

πŸ“Œ Note: If the radio buttons belong to the same group (same name attribute), selecting one automatically deselects the others.


How to locate a Radio Button using an ID locator?

To locate and select a radio button using ID, use the following approach:

WebElement radioBtn = driver.findElement(By.id("radioButtonId"));
radioBtn.click();  // Selects the radio button

πŸ“Œ ID Locator: The id is unique, making it the most efficient way to find an element.


How to locate a Radio Button using the Name locator?

If multiple radio buttons share the same name attribute, use findElements() to get the list and select the desired option:

List<WebElement> radioButtons = driver.findElements(By.name("gender"));
radioButtons.get(1).click();  // Selects the second radio button in the group

πŸ“Œ Use Case: When selecting a radio button from a group with the same name, this approach helps in choosing a specific one.


How to locate a Radio Button using the XPath locator?

To locate a radio button using XPath, use the following approach:

WebElement radioBtn = driver.findElement(By.xpath("//input[@type='radio' and @id='radioButtonId']"));
radioBtn.click();  // Selects the radio button

πŸ’‘ XPath Explanation:

  • "//input[@type='radio']" selects all radio buttons.
  • "@id='radioButtonId'" filters the radio button by its id.

How to locate a Radio Button using the CSS Selector locator?

To locate a radio button using CSS Selector, use the following:

WebElement radioBtn = driver.findElement(By.cssSelector("input[type='radio']#radioButtonId"));
radioBtn.click();  // Selects the radio button

πŸ“Œ CSS Selector Explanation:

  • "input[type='radio']" selects all radio buttons.
  • "#radioButtonId" targets a specific radio button by id.

How to perform validations on Radio Buttons in Selenium WebDriver?

You can validate radio buttons using:
βœ”οΈ .isSelected() – Checks if the radio button is selected.
βœ”οΈ .isDisplayed() – Checks if the radio button is visible on the page.
βœ”οΈ .isEnabled() – Checks if the radio button is enabled and interactable.


How to verify if the Radio Button is selected using the isSelected() method?

The isSelected() method returns true if the radio button is selected.

WebElement radioBtn = driver.findElement(By.id("radioButtonId"));
if (radioBtn.isSelected()) {
    System.out.println("Radio Button is already selected.");
} else {
    radioBtn.click();
    System.out.println("Radio Button is now selected.");
}

πŸ“Œ Use Case: Prevents unnecessary clicks on an already selected radio button.


How to verify if the Radio Button is displayed using the isDisplayed() method?

The isDisplayed() method checks if the radio button is visible on the page.

WebElement radioBtn = driver.findElement(By.id("radioButtonId"));
if (radioBtn.isDisplayed()) {
    System.out.println("Radio Button is visible.");
} else {
    System.out.println("Radio Button is not visible.");
}

πŸ“Œ Use Case: Useful for confirming the presence of an element before interacting with it.


How to verify if the Radio Button is enabled using the isEnabled() method?

The isEnabled() method checks if the radio button is enabled for selection.

WebElement radioBtn = driver.findElement(By.id("radioButtonId"));
if (radioBtn.isEnabled()) {
    radioBtn.click();
    System.out.println("Radio Button is enabled and selected.");
} else {
    System.out.println("Radio Button is disabled and cannot be selected.");
}

πŸ“Œ Use Case: Ensures the radio button is not disabled before trying to click it.

Handling Multiple Radio Buttons Dynamically in Selenium

When a webpage has multiple radio buttons in a group (e.g., selecting gender: Male, Female, Other), we can handle them dynamically in Selenium using findElements().


Example: Selecting a Specific Radio Button from a Group

Suppose we have multiple radio buttons under the same name attribute, and we want to select a specific one.

HTML Example

<input type="radio" name="gender" value="Male"> Male
<input type="radio" name="gender" value="Female"> Female
<input type="radio" name="gender" value="Other"> Other

Selenium Code (Java)

// Locate all radio buttons with the same 'name' attribute
List<WebElement> radioButtons = driver.findElements(By.name("gender"));

// Loop through each radio button and select the one with value "Female"
for (WebElement radio : radioButtons) {
    if (radio.getAttribute("value").equals("Female")) {
        radio.click();  // Clicks the Female radio button
        System.out.println("Female option selected.");
        break;
    }
}

βœ… Explanation:

  • findElements(By.name("gender")) gets all radio buttons with the same name.
  • A for loop iterates through them, checking their value attribute.
  • If the value matches "Female", the .click() method selects it.

Example: Selecting a Radio Button Dynamically by Index

If we don’t know the values beforehand, we can select a radio button based on its position (index).

// Locate all radio buttons in a group
List<WebElement> radioButtons = driver.findElements(By.name("gender"));

// Select the second radio button (index 1) -> Female
radioButtons.get(1).click();

System.out.println("Selected radio button: " + radioButtons.get(1).getAttribute("value"));

πŸ“Œ Note: Indexing starts from 0, so get(1) selects the second option.


Example: Verifying Which Radio Button is Selected

List<WebElement> radioButtons = driver.findElements(By.name("gender"));

for (WebElement radio : radioButtons) {
    if (radio.isSelected()) {
        System.out.println("Currently selected option: " + radio.getAttribute("value"));
        break;
    }
}

βœ… Explanation:

  • Iterates through all radio buttons.
  • Uses .isSelected() to find the checked radio button.
  • Prints the value of the selected option.

Example: Selecting a Radio Button If It’s Not Already Selected

WebElement radioButton = driver.findElement(By.xpath("//input[@value='Male']"));
if (!radioButton.isSelected()) {
    radioButton.click();
    System.out.println("Male radio button is now selected.");
} else {
    System.out.println("Male radio button was already selected.");
}

πŸ“Œ Use Case: Prevents unnecessary clicks if the radio button is already selected.


Example: Handling Disabled Radio Buttons

Sometimes, radio buttons may be disabled. Use .isEnabled() to check before clicking.

WebElement radioButton = driver.findElement(By.xpath("//input[@value='Other']"));

if (radioButton.isEnabled()) {
    radioButton.click();
    System.out.println("Other option selected.");
} else {
    System.out.println("The 'Other' radio button is disabled.");
}

Summary

βœ”οΈ Finding multiple radio buttons β†’ Use findElements().
βœ”οΈ Selecting dynamically β†’ Loop through elements using getAttribute("value").
βœ”οΈ Selecting by index β†’ Use get(index).
βœ”οΈ Checking selection β†’ Use .isSelected().
βœ”οΈ Handling disabled buttons β†’ Use .isEnabled().
βœ”οΈ A Radio Button allows selecting only one option from a group.
βœ”οΈ Selenium WebDriver provides multiple ways to locate radio buttons (ID, Name, XPath, CSS Selector).
βœ”οΈ We can select a radio button using the .click() method.
βœ”οΈ Validation methods:

  • .isSelected() β†’ Checks if a radio button is selected.
  • .isDisplayed() β†’ Checks if a radio button is visible.
  • .isEnabled() β†’ Checks if a radio button is active.