Introduction
A Radio Button is a form element that allows users to select one option from a group of available choices. Unlike checkboxes, where multiple selections are possible, only one radio button can be selected at a time within the same group.
Radio buttons are commonly used in registration forms, surveys, payment methods, gender selection, and preference settings.
In Selenium, radio buttons are typically automated using the click() method to select an option and is_selected() to verify whether the desired option has been selected successfully.
In this tutorial, you’ll learn how to locate and interact with radio buttons using Selenium with Python, along with practical examples, real-world scenarios, common mistakes, and best practices.
What is a Radio Button?
A Radio Button is an HTML input element that allows users to select only one option from a group.
Common examples include:
Gender Selection
Payment Methods
Shipping Options
Subscription Plans
Survey Responses
Language Preferences
Example HTML:
<input type="radio" value="Male" name="gender">
<input type="radio" value="Female" name="gender">
Since both radio buttons belong to the same group (name="gender"), selecting one option automatically deselects the other.
Why Automate Radio Buttons?
Automating radio buttons helps you:
Verify form functionality.
Validate user selections.
Perform end-to-end workflow testing.
Test business rules that depend on user choices.
Improve automation coverage.
Common Methods Used
| Method | Purpose |
|---|---|
| click() | Select the radio button |
| is_selected() | Verify whether the radio button is selected |
| is_displayed() | Verify that the radio button is visible |
| is_enabled() | Verify that the radio button is enabled |
| get_attribute() | Retrieve radio button attributes |
Example
The following example selects the Male radio button on the Selenium Playground website and verifies that the option was selected successfully.
from selenium import webdriver
from selenium.webdriver.common.by import By
# Topic: 25. Form Elements - Radio Buttons
# Practice site: https://www.testmuai.com/selenium-playground/radiobutton-demo
# Run: pytest -s 25_examples/test_03_radio_buttons.py
#
# Radio buttons allow selecting one option from a group. Only one radio in a
# group can be selected at a time.
def test_radio_buttons():
driver = webdriver.Chrome()
try:
driver.get("https://www.testmuai.com/selenium-playground/radiobutton-demo")
male_radio = driver.find_element(
By.CSS_SELECTOR,
"input[value='Male']"
)
male_radio.click()
assert male_radio.is_selected()
finally:
driver.quit()
Output
The Male radio button is selected successfully.
Understanding the Code
Import the Required Classes
from selenium import webdriver
from selenium.webdriver.common.by import By
Imports:
webdriverfor launching and controlling the browser.Byfor locating web elements using Selenium’s locator strategies.
Create the WebDriver
driver = webdriver.Chrome()
Launches a new Chrome browser session.
Open the Practice Website
driver.get(
"https://www.testmuai.com/selenium-playground/radiobutton-demo"
)
Opens the Selenium Playground webpage containing multiple radio button examples.
Locate the Radio Button
male_radio = driver.find_element(
By.CSS_SELECTOR,
"input[value='Male']"
)
Locates the Male radio button using its value attribute.
Using CSS Selectors provides a simple and reliable way to locate radio buttons when stable attributes are available.
Select the Radio Button
male_radio.click()
The click() method simulates a mouse click and selects the radio button.
Since radio buttons allow only one selection within a group:
Selecting one option automatically deselects the previously selected option.
Only one radio button can remain selected at a time.
Verify the Selection
assert male_radio.is_selected()
The is_selected() method verifies whether the radio button is currently selected.
If the assertion passes successfully, it confirms that Selenium selected the desired option correctly.
Close the Browser
driver.quit()
Closes the browser and ends the WebDriver session.
This is a recommended practice to ensure that all browser instances are properly terminated after test execution.
Verifying Whether a Radio Button is Visible
Before selecting a radio button, you can verify whether it is visible on the webpage.
Example:
male_radio.is_displayed()
This helps prevent failures caused by hidden elements.
Verifying Whether a Radio Button is Enabled
Sometimes radio buttons remain disabled until certain conditions are satisfied.
Example:
male_radio.is_enabled()
This method verifies whether the radio button is ready for interaction.
Checking Whether a Radio Button is Selected
You can verify the current selection status using:
male_radio.is_selected()
Output:
True
This is particularly useful when validating user preferences and application behavior.
Practical Example
Suppose an e-commerce website allows users to select a payment method using radio buttons.
The automation script:
Selects Credit Card.
Proceeds to the payment page.
Verifies that the selected payment option is preserved successfully.
This validates both the radio button functionality and the application’s business workflow.
Automation Testing Example
Consider an online banking application.
The account creation form contains radio buttons for:
Savings Account
Current Account
Salary Account
The automation script:
Selects the desired account type.
Submits the registration form.
Verifies that the selected option is processed successfully.
Radio buttons are frequently used in business-critical workflows that require mutually exclusive choices.
Real-World Example
Radio buttons are commonly used in:
Banking applications
E-commerce websites
CRM systems
Healthcare portals
HR management systems
Government websites
Enterprise web applications
They are particularly useful whenever users must select exactly one option from a predefined list.
Advantages of Automating Radio Buttons
Simulates real user selections.
Validates business workflows.
Improves automation coverage.
Supports end-to-end testing.
Reduces manual testing effort.
Common Mistakes Beginners Make
Selecting Multiple Options
Remember that only one radio button can be selected within the same group.
Selecting another option automatically deselects the previous one.
Using Incorrect Locators
Always prefer stable locators such as:
ID
Name
CSS Selector
Avoid fragile XPath expressions whenever possible.
Ignoring Synchronization
Radio buttons may load dynamically after API calls or animations.
Use Explicit Wait whenever necessary before interacting with them.
Forgetting to Verify the Selection
Always verify that the correct radio button was selected.
Example:
radio_button.is_selected()
Best Practices
Prefer locating radio buttons using ID, Name, or CSS Selector.
Verify that radio buttons are visible and enabled before interacting with them.
Use Explicit Wait for dynamically loaded elements.
Validate the selected option using
is_selected().Use reliable and maintainable locators.
Conclusion
Radio buttons are among the most commonly used form elements in Selenium automation whenever users must select one option from a group of choices. By using methods such as click() and is_selected(), you can reliably automate user selections and validate application workflows. Following best practices such as using stable locators and proper synchronization helps create robust and maintainable automation scripts.
Frequently Asked Questions (FAQs)
Which method is used to select a radio button?
Use:
click()
How can I verify whether a radio button is selected?
Use:
is_selected()
Can multiple radio buttons be selected simultaneously?
No.
Only one radio button can be selected at a time within the same group.
Which locator is best for locating radio buttons?
The preferred locators are:
ID
Name
CSS Selector
Are radio buttons commonly automated in Selenium?
Yes.
Radio buttons are frequently used in registration forms, payment workflows, surveys, and preference settings across modern web applications.
Key Takeaways
Radio buttons allow users to select only one option from a group.
Use
click()to select a radio button.Use
is_selected()to verify the selected option.Prefer stable locators such as ID, Name, and CSS Selector.
Use Explicit Wait for dynamically loaded radio buttons.
Validate application behavior after making selections.
Proper synchronization and locator selection improve automation reliability.
