Selenium Radio Button in Java with practical examples

Selenium Radio Button Is Selected with Java Examples


Radio buttons are an essential component of web forms that allow users to make a single selection from a list of options. When it comes to automated testing using Selenium and Java, it’s crucial to understand how to interact with radio buttons effectively. In this article, we will explore different methods to select a radio button using Selenium and provide Java examples to demonstrate their usage.

1. Understanding Radio Buttons

Radio buttons, also known as option buttons, allow users to choose a single option from a set of mutually exclusive choices. Only one radio button within a group can be selected at a time. They are commonly used in web forms for selecting preferences, gender, or other categorical options. It appears as a small circle or dot that can be either filled or empty. When a user selects one radio button, it automatically deselects any previously selected radio buttons within the same group. Radio buttons are commonly used in forms and surveys to capture user preferences or choices.

For example, let’s say you’re filling out a registration form for a conference. The form includes a question asking for your meal preference: vegetarian or non-vegetarian. Next to each option, there will be a radio button. You can select the radio button next to your preferred choice, and it will indicate your selection visually. If you change your mind and select the other option, the previously selected radio button will automatically be deselected.

In HTML, radio buttons are created using the <input> element with the type="radio" attribute. Each radio button needs a unique name attribute to group them together. Here’s an example:

    <input type="radio" name="gender" id="male" value="male">
    <label for="male">Male</label>
    <input type="radio" name="gender" id="female" value="female">
    <label for="female">Female</label>
    <input type="radio" name="gender" id="other" value="other">
    <label for="other">Other</label>

In this example, we have three radio buttons representing different gender options. They are grouped together by giving them the same name attribute, which is “gender” in this case. The id attribute is used to associate each label with its corresponding radio button using the for attribute.

When the user selects one radio button, the others in the same group automatically become deselected. To retrieve the selected value in a form submission, you can use server-side programming or JavaScript.

Remember to replace the value attribute with the appropriate values for your specific use case. You can also style the radio buttons using CSS to match the design of your web page. This is a basic example of radio buttons in HTML.

In summary, a radio button is a user interface element that allows users to select a single option from a set of choices. It helps streamline user input and ensures that only one option can be selected at a time.

3. Locating Radio Buttons using selenium radio button locators indentification techniques:

To select a radio button, we first need to locate it on the web page. Selenium provides various methods to locate elements, including radio buttons. Some commonly used locators are:

  • By ID
  • By XPath
  • By CSS Selector

Using these locators, we can identify the radio button element uniquely.

4. Selecting a Radio Button

Once we have located the radio button element, we can use the Selenium WebDriver’s click() method to select it. Here’s an example of selenium radio button:

WebElement radioBtn = driver.findElement("male"));;

This code snippet finds the radio button with the specified ID and clicks on it to select it.

5. Verifying the Selected Radio Button

After selecting a radio button, it’s important to verify whether it has been selected successfully. We can use the isSelected() method provided by Selenium WebDriver to check the selection status. Here’s an example:

WebElement radioBtn = driver.findElement("radioButton1"));;
boolean isSelected = radioBtn.isSelected();

The isSelected() method returns a boolean value indicating whether the radio button is selected or not.

6. Handling Radio Button Groups

Till now we have seen how selenium radio button works. But, radio buttons are often grouped together to represent related options. To select a radio button from a group, we need to identify the group and choose the desired option. Here’s an example:

List<WebElement> radioBtns = driver.findElements("gender"));
for (WebElement radioBtn : radioBtns) {
    if (radioBtn.getAttribute("value").equals("male")) {;

In this code snippet, we locate all the radio buttons within the group and iterate through them to find the desired option. Once found, we click on it to select.

7. Advanced Techniques for Radio Button Selection

Besides the basic selection methods, Selenium provides additional techniques to handle selenium radio buttons effectively. here we can see how to select radio button in selenium without id. Some of these techniques include:

  • Selecting by index
  • Selecting by value attribute
  • Selecting by label text
  • Selecting by sibling or ancestor relationship

Here are examples of selecting a radio button in Selenium using different methods:

  1. Selecting by Index:
List<WebElement> radioButtons = driver.findElements("gender"));
int desiredIndex = 2; // Index of the radio button you want to select

if (desiredIndex >= 0 && desiredIndex < radioButtons.size()) {
} else {
    System.out.println("Invalid index specified");
  1. Selecting by Value Attribute:
WebElement radioButton = driver.findElement(By.cssSelector("input[value='male']"));;

In this example, we locate the radio button by its value attribute, which is set to “male”. You can replace “male” with the desired value of the radio button you want to select.

  1. Selecting by Label Text: This example will also show how to select radio button in selenium using xpath :
WebElement radioButton = driver.findElement(By.xpath("//label[text()='Female']/preceding-sibling::input"));;

Here, we locate the radio button based on its associated label text. In this case, we select the radio button with the label text “Female”. The XPath expression finds the preceding sibling input element of the label.

  1. Selecting by Sibling or Ancestor Relationship:
WebElement radioButton = driver.findElement(By.xpath("//input[@id='other']/following-sibling::label"));;

In this example, we locate the radio button based on its sibling relationship with a label element. The XPath expression finds the label element that follows the input element with the id “other”.

These examples demonstrate different approaches for selecting a radio button in Selenium using various criteria such as index, value attribute, label text, and sibling/ancestor relationships. Choose the method that best suits your specific scenario and replace the locators or values accordingly.

These advanced techniques offer more flexibility in selecting radio buttons based on specific criteria.

8. Best Practices for Testing Radio Buttons

When testing radio buttons with Selenium, it’s important to follow best practices to ensure effective and reliable test automation. Here are some recommendations:

  • Use unique and descriptive locators for radio buttons.
  • Verify the selection status after performing a click operation.
  • Consider different scenarios, such as selecting the first option, last option, or an intermediate option.
  • Test both the selection and deselection of radio buttons.
  • Implement appropriate wait conditions to synchronize with the page load.

By following these best practices, you can create robust and maintainable test scripts for radio button interactions.

9. Checkbox versus Radio Buttons

While checkboxes allow for multiple selections, radio buttons are used when users need to select only one option from a group. Radio buttons are represented by circular buttons, and only one option can be selected at a time. It’s important to use checkboxes when users need to select multiple options and radio buttons when only one option is allowed

10. Conclusion

In this article, we explored the process of selecting radio buttons using Selenium and Java. We learned about locating radio buttons, selecting them, verifying the selection status, handling radio button groups, and utilizing advanced techniques. By applying these techniques in your Selenium automation tests, you can ensure accurate and reliable interaction with radio buttons on web forms.


1. How do I locate a radio button using XPath?

To locate a radio button using XPath, you can use the By.xpath() method in Selenium. Provide the XPath expression that uniquely identifies the radio button element.

2. Can I select multiple radio buttons at once?

No, radio buttons are designed to allow only a single selection. Selecting one radio button automatically deselects the previously selected radio button within the same group.

3. What happens if I don’t verify the selection status after clicking on a radio button?

It is recommended to verify the selection status after clicking on a radio button to ensure the expected behavior. If you skip this verification, there is a possibility of false positives or false negatives in your tests.

4. Are there any alternative frameworks to Selenium for testing radio buttons?

Yes, there are alternative frameworks such as Cypress and TestCafe that can be used for testing radio buttons and other web elements. However, Selenium remains one of the most popular and widely adopted frameworks for web automation testing.

5. How can I handle dynamically generated radio buttons?

If the radio buttons are dynamically generated, you can use dynamic locators or find elements within a specific container or context to locate and interact with them during runtime. In selenium radio buttons are handled by creating dynamic xpath.

In conclusion, understanding how to select radio buttons using Selenium and Java is crucial for effective web automation testing. By following the outlined methods and best practices, you can write reliable and maintainable test scripts that ensure accurate interaction with radio buttons on web forms. Remember to customize the code according to your specific web application requirements. Happy automation!

Leave a Reply