Skip to content
CodeKruPro

Choose One Radio in a Group

27/80

Choose One Radio in a Group

Radios that share a name are one group. Selecting the second clears the first.

Task:

  1. On Radio Buttons, read #firstSelect1
  2. Click it and read it again
  3. Click #secondSelect1 and print both states

Print the lines below exactly.

Expected output:

First initially: False
First after click: True
After selecting second - First: False
After selecting second - Second: True

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

  1. Open the Radio Buttons page and locate the first and second radios from the same group by id. Store each in a variable.
  2. Print the first radio's state before any click, then click it and print its state again. Each line has its own label.
  3. Click the second radio.
  4. 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 returns True. 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 a TypeError. Wrap it in str().

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.

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.

main.py
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()