How to get the selected radio button in Selenium Python?

Selenium Python – Get the selected radio button

In this tutorial, you will learn how to get the selected radio button in a group of related radio buttons using Selenium in Python.

To get the selected radio button in Selenium Python, find the radio button in the group which has been checked using XPath expression. Once you’ve located the selected radio button, you can get the value of the radio button.

In the following code snippet, we get the selected radio button from a radio buttons group with name 'radio-group-name' is

radio_button = driver.find_element(By.XPATH, "//input[@type='radio' and @name='radio-group-name' and @checked]")

In the above XPath expression

  • //input returns all the elements with input tag.
  • @type='radio' filters only those elements that are radio buttons.
  • @name='radio-group-name' filters only those radio buttons whose name attribute has a value equal to 'radio-group-name'.
  • @checked further filters only those radio buttons that are checked.
  • driver.find_element() returns the first radio button from the filtered list.

You can get the value of the radio button using the following code.

selected_value = radio_button.get_attribute("value")

Example

In this example, we shall consider loading the webpage at URL: https://pythonexamples.org/tmp/selenium/index-17.html. The contents of this HTML file is given below.

The webpage has a radio button with value="Mathematics" that has already been checked in the HTML using checked attribute.

index.html

<html>
 <body>
  <h3>My Favourite Subject</h3>
  <form action="">
    <input type="radio" id="physics" name="fav_subject" value="Physics">
    <label for="physics">Physics</label><br>
    <input type="radio" id="mathematics" name="fav_subject" value="Mathematics" checked>
    <label for="mathematics">Mathematics</label><br>
    <input type="radio" id="chemistry" name="fav_subject" value="Chemistry">
    <label for="chemistry">Chemistry</label>
  </form>
 </body>
</html>
Copy
Selenium Python - Get the selected radio button

In the following program, we initialize a webdriver, navigate to the specified URL, and get the radio button with name equal to "fav_subject" and attribute checked. We shall print the value of the selected radio button to standard console output.

Python Program

from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.service import Service as ChromeService
from selenium.webdriver.common.by import By

# Setup chrome driver
driver = webdriver.Chrome(service=ChromeService(executable_path=ChromeDriverManager().install()))

# Navigate to the url
driver.get('https://pythonexamples.org/tmp/selenium/index-17.html')

# Get the selected radio button
radio_button = driver.find_element(By.XPATH, "//input[@type='radio' and @name='fav_subject' and @checked]")

# Get the value of selected radio button
selected_value = radio_button.get_attribute("value")
print("Selected radio button :", selected_value)

# Close the driver
driver.quit()
Copy

Output

Selected radio button : Mathematics

Summary

In this Python Selenium tutorial, we have given instructions on how to get the selected radio button, and print its value, with an example.

Quiz on Selenium

Q1. What is a web driver in Selenium Python?

Not answered

Q2. Which of the following is not a popular web driver used in Selenium Python?

Not answered

Q3. What is the method used to find web elements in Selenium Python?

Not answered

Q4. What is the method used to interact with web elements in Selenium Python?

Not answered

Q5. Which of the following is not a method to wait for a web element to load in Selenium Python?

Not answered
Code copied to clipboard successfully 👍