Radio button interactions in Selenium Python - Build an Automation Script
from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC import unittest class PreferencesPage: def __init__(self, driver): self.driver = driver self.monthly_radio = (By.ID, 'subscription_monthly') self.yearly_radio = (By.ID, 'subscription_yearly') def select_monthly(self): WebDriverWait(self.driver, 10).until( EC.element_to_be_clickable(self.monthly_radio) ).click() def select_yearly(self): WebDriverWait(self.driver, 10).until( EC.element_to_be_clickable(self.yearly_radio) ).click() def is_monthly_selected(self): return self.driver.find_element(*self.monthly_radio).is_selected() def is_yearly_selected(self): return self.driver.find_element(*self.yearly_radio).is_selected() class TestRadioButtonSelection(unittest.TestCase): def setUp(self): self.driver = webdriver.Chrome() self.driver.get('http://example.com/preferences') self.page = PreferencesPage(self.driver) def tearDown(self): self.driver.quit() def test_radio_button_selection(self): # Select Monthly and verify self.page.select_monthly() self.assertTrue(self.page.is_monthly_selected(), "Monthly radio button should be selected") self.assertFalse(self.page.is_yearly_selected(), "Yearly radio button should not be selected when Monthly is selected") # Select Yearly and verify self.page.select_yearly() self.assertTrue(self.page.is_yearly_selected(), "Yearly radio button should be selected") self.assertFalse(self.page.is_monthly_selected(), "Monthly radio button should not be selected when Yearly is selected") if __name__ == '__main__': unittest.main()
This test script uses Selenium with Python and unittest framework.
The PreferencesPage class is a Page Object that holds locators and actions for the radio buttons. It uses By.ID locators for the 'Monthly' and 'Yearly' radio buttons, which is a best practice for stable selectors.
Explicit waits (WebDriverWait) ensure the radio buttons are clickable before clicking, avoiding flaky tests.
The test test_radio_button_selection selects 'Monthly' first and asserts it is selected while 'Yearly' is not. Then it selects 'Yearly' and asserts the opposite. This confirms only one radio button is selected at a time.
Assertions have clear messages to help understand failures.
The setUp and tearDown methods handle browser start and cleanup.
This structure keeps test logic clean and maintainable.
Now add data-driven testing with 3 different subscription types: 'Monthly', 'Yearly', and 'Weekly' (assume 'Weekly' radio button exists with ID 'subscription_weekly').
Practice
is_selected() method do when used on a radio button element in Selenium?Solution
Step 1: Understand the purpose of
This method checks the current state of a radio button or checkbox to see if it is selected or not.is_selected()Step 2: Compare with other actions
Unlikeclick()which changes the state,is_selected()only returns a boolean indicating selection status.Final Answer:
Checks if the radio button is currently selected -> Option AQuick Check:
Check selection status = C [OK]
- Confusing is_selected() with click()
- Expecting is_selected() to select the button
- Using is_selected() to get button text
Solution
Step 1: Identify the correct method to select a radio button
In Selenium,click()is used to select radio buttons or checkboxes.Step 2: Verify method names
Methods likeselect(),check(), orchoose()do not exist in Selenium WebDriver API.Final Answer:
driver.find_element(By.ID, 'option1').click() -> Option DQuick Check:
Use click() to select radio buttons = A [OK]
- Using non-existent methods like select()
- Trying to assign value instead of clicking
- Confusing with form select elements
radio1 = driver.find_element(By.ID, 'r1') radio2 = driver.find_element(By.ID, 'r2') radio1.click() print(radio1.is_selected()) print(radio2.is_selected())
What will be the output if these are radio buttons in the same group?
Solution
Step 1: Understand radio button group behavior
Clicking one radio button selects it and deselects others in the same group.Step 2: Analyze the code output
After clickingradio1,radio1.is_selected()returns True, andradio2.is_selected()returns False.Final Answer:
True False -> Option AQuick Check:
One selected, others deselected = B [OK]
- Assuming both radio buttons can be selected
- Confusing is_selected() output order
- Ignoring group behavior of radio buttons
radio = driver.find_element(By.NAME, 'gender') radio.click()
But it always selects the first radio button in the group, not the intended one. What is the likely problem?
Solution
Step 1: Understand how
This method returns the first matching element with the given name attribute.find_element(By.NAME)worksStep 2: Identify the issue with multiple radio buttons sharing the same name
Radio buttons in a group share the same name, so you must select the correct one by index or another attribute.Final Answer:
Using By.NAME returns multiple elements; need to specify which one -> Option BQuick Check:
By.NAME returns first element only = A [OK]
- Assuming By.NAME returns all elements
- Using click() incorrectly
- Ignoring radio button group naming
Solution
Step 1: Select all radio buttons by group name
Usefind_elements(By.NAME, 'color')to get all radio buttons in the group.Step 2: Click one button and assert selection states
Click the second button, then assert it is selected and the first is not.Final Answer:
radio_buttons = driver.find_elements(By.NAME, 'color') radio_buttons[1].click() assert radio_buttons[1].is_selected() and not radio_buttons[0].is_selected() -> Option CQuick Check:
Click one, check selected and deselected = D [OK]
- Using find_element instead of find_elements
- Not checking deselection of other buttons
- Wrong locator type for radio group
Ready to prove it?
Pass the test and this pattern moves unknown
