About this lesson
Select a radio button in Selenium Python and watch the group with is_selected
Selenium Python AssignmentsIntermediateCheckboxes and Radio Buttons
How it works
A radio button is an input with type set to radio. Radios that share the same name attribute form one group, and the browser allows only one of them to be on at a time. On this practice page three radios share one name, and a search by that name finds all three. After you click one, exactly one of them reports True from is_selected().
Clicking is not a toggle here. Clicking a radio that is already selected leaves it selected, and nothing in Selenium raises a warning. To change the choice you click a different radio of the same group, and the browser clears the earlier one for you. You never untick a radio yourself.
Python reminders apply. is_selected() returns a bool, so each value has to go through str() before you join it to a label. Keep each radio in its own variable, because you will read the first one three times: at the start, after clicking it, and after the second one is clicked.
About the assignment
Prove the one-at-a-time rule of a radio group. You select the first radio, then the second, and report how the first reacts.
What you will do
- Open the Radio Buttons page and locate the first and second radios from the same group by id. Store each in a variable.
- Print the first radio's state before any click, then click it and print its state again. Each line has its own label.
- Click the second radio.
- Print the state of the first radio and then the second, using the two labels from the task. Convert each bool with
str().
Common mistakes
Clicking the first radio again to clear it
A second click on a selected radio changes nothing.
is_selected()still returnsTrue. To clear it, select another radio in the same group.Reading the first radio only once
If you store the earlier answer and print it later, the report shows the old state. Call
is_selected()again after the second click.Concatenating the bool
Adding the result of
is_selected()to a string fails with aTypeError. Wrap it instr().
In a real test suite
In a suite, radio choices are usually picked by label or value from test data, often with pytest.mark.parametrize running the same check for each option. The group rule means each case only needs to assert on the radio it picked.
The next lesson moves back to checkboxes, but with a control that ignores clicks completely.
Related lessons
- Find Every Radio ButtonFinds every radio button with a list locator, which this lesson builds on.
- Select a CheckboxUses the same
is_selected()read on a checkbox that toggles. - Clear a Checkbox That Starts SelectedShows the contrast: a checkbox can be unticked by a click, a radio cannot.
To try one of these calls on any page without a task, open the Online Selenium Python Compiler.
Starter code
The editor above opens with this code. Fill in the TODO lines, then run and submit it.
from selenium import webdriver
from selenium.webdriver.common.by import By
driver = webdriver.Chrome()
try:
driver.get("https://testkru.com/Elements/RadioButtons")
# Select the first radio, then the second, and print the four states
finally:
driver.quit()