How to select a radio button by value in Selenium Python?

Selenium Python – Select a radio button by value

In this tutorial, you will learn how to select a radio button by value in value attribute, in Selenium Python.

To select a radio button by value in Selenium Python, form the XPath expression that the element has the following two attribute value.

  • type=”radio”
  • value=”your_value”

The XPath expression would be as shown in the following.

"//input[@name='x' and @value='your_value']"

You can use this XPath expression with find_element() method of driver object, and find the radio button with a specific value for value attribute.

radio_button = driver.find_element("//input[@name='x' and @value='your_value']")

After finding the radio button, call click() method on the radio button WebElement object.

radio_button.click()

Example

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

The webpage has a radio group for selecting a favorite subject.

<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">
    <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

In the following program, we initialize a webdriver, navigate to a specified URL, get the radio button with value equal to "Chemisty", and call click() method on the radio button element.

We shall take screenshots before and after selecting the radio button.

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
service = ChromeService(executable_path=ChromeDriverManager().install())
driver = webdriver.Chrome(service=service)
driver.set_window_size(500, 400)

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

# Take a screenshot
driver.save_screenshot("screenshot-1.png")

# Get the radio button whose value="Chemistry"
radio_button = driver.find_element(By.XPATH, "//input[@type='radio' and @value='Chemistry']")

# Click the radio button
radio_button.click()

# Take a screenshot
driver.save_screenshot("screenshot-2.png")

# Close the driver
driver.quit()
Copy

screenshot-1.png – Before selecting the radio button

Python Selenium - Screenshot before selecting the radio button

screenshot-2.png – After selecting the radio button

Python Selenium - Screenshot after selecting the radio button based on value attribute

Summary

In this Python Selenium tutorial, we have given instructions on how to select a radio button by value attribute, with an example.

Related Tutorials

Quiz on Selenium

Q1. What is Selenium Python used for?

Not answered

Q2. What is a web driver in Selenium Python?

Not answered

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

Not answered

Q4. What is the method used to navigate back to the previous page in Selenium Python?

Not answered

Q5. What is the method used to take a screenshot in Selenium Python?

Not answered
Code copied to clipboard successfully 👍