Last modified: Dec 05, 2023 By Alexander Williams

Python Selenium: Select Radio Button - Examples

Example 1: Select Radio Button by ID


from selenium import webdriver

# Create a Chrome driver
driver = webdriver.Chrome()

# Navigate to a webpage with radio buttons
driver.get('https://example.com')

# Find the radio button by its ID
radio_button = driver.find_element_by_id('radio-button-id')

# Select the radio button
radio_button.click()

Output:


# The radio button on the webpage is selected.

Example 2: Select Radio Button by Value


# Continue from the previous example

# Find the radio button by its value attribute
radio_button = driver.find_element_by_xpath('//input[@value="radio-value"]')

# Select the radio button
radio_button.click()

Output:


# The radio button on the webpage is selected.

Example 3: Select Radio Button in a Group


# Continue from the previous example

# Find all radio buttons in the group by the shared name attribute
radio_buttons = driver.find_elements_by_name('group-name')

# Select a specific radio button in the group
radio_buttons[1].click()

Output:


# The second radio button in the group on the webpage is selected.

Example 4: Select Radio Button with Explicit Wait


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

# Continue from the first example

# Define an explicit wait for the radio button to be clickable
radio_button_locator = (By.ID, 'radio-button-id')
radio_button = WebDriverWait(driver, 10).until(EC.element_to_be_clickable(radio_button_locator))

# Select the radio button
radio_button.click()

Output:


# The radio button on the webpage is selected.