0
0
Selenium Pythontesting~5 mins

Radio button interactions in Selenium Python

Choose your learning style9 modes available
Introduction

Radio buttons let users pick one choice from many. Testing them ensures the right option is selected and works as expected.

When testing a form where only one option can be chosen, like gender selection.
When verifying that selecting one radio button deselects others in the group.
When checking if the default radio button is selected on page load.
When ensuring that clicking a radio button triggers the correct action or message.
Syntax
Selenium Python
from selenium import webdriver
from selenium.webdriver.common.by import By

# Find radio button element
radio_button = driver.find_element(By.ID, 'radio_id')

# Click to select
radio_button.click()

# Check if selected
is_selected = radio_button.is_selected()

Use unique locators like ID or name to find radio buttons reliably.

Use is_selected() to verify if a radio button is chosen.

Examples
Selects the radio button with ID 'male'.
Selenium Python
radio_male = driver.find_element(By.ID, 'male')
radio_male.click()
Finds radio button by name 'gender' and clicks it if not already selected.
Selenium Python
radio_female = driver.find_element(By.NAME, 'gender')
if not radio_female.is_selected():
    radio_female.click()
Selects the radio button with value 'blue' among options named 'color'.
Selenium Python
radio_options = driver.find_elements(By.NAME, 'color')
for option in radio_options:
    if option.get_attribute('value') == 'blue':
        option.click()
        break
Sample Program

This script opens a simple page with three radio buttons. It clicks the second option and checks that it is selected while the first is not. It prints a success message if all checks pass.

Selenium Python
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
import time

# Setup Chrome driver (adjust path as needed)
options = Options()
options.add_argument('--headless')
service = Service()
driver = webdriver.Chrome(service=service, options=options)

try:
    # Open a simple HTML page with radio buttons
    html = '''
    <html>
    <body>
      <form>
        <input type='radio' id='option1' name='choice' value='1'> Option 1<br>
        <input type='radio' id='option2' name='choice' value='2'> Option 2<br>
        <input type='radio' id='option3' name='choice' value='3'> Option 3<br>
      </form>
    </body>
    </html>
    '''
    driver.get('data:text/html;charset=utf-8,' + html)

    # Select Option 2
    option2 = driver.find_element(By.ID, 'option2')
    option2.click()

    # Verify Option 2 is selected
    assert option2.is_selected(), 'Option 2 should be selected'

    # Verify Option 1 is not selected
    option1 = driver.find_element(By.ID, 'option1')
    assert not option1.is_selected(), 'Option 1 should not be selected'

    print('Test passed: Radio button selection works correctly')
finally:
    driver.quit()
OutputSuccess
Important Notes

Always wait for the page to load before interacting with elements.

Use unique and stable locators to avoid flaky tests.

Clicking a radio button automatically deselects others in the same group.

Summary

Radio buttons allow only one choice in a group.

Use click() to select and is_selected() to check selection.

Test that selecting one radio button deselects others.