How to select the second radio button in Selenium Python?

Selenium Python – Select the second radio button

In this tutorial, you will learn how to select the second radio button in a radio group in a form, in Selenium Python.

To select the second radio button in Selenium Python, find the radio buttons in the group using appropriate selector such as an ID or XPath expression. Once you’ve located the radio buttons, you can then get the second radio button using array notation and index=1, call the click() method on the second radio button element.

In the following code snippet, we find the second radio button in the radio group whose name attribute is 'x', and select the second radio button using click() method.

radio_buttons = driver.find_elements("//input[@name='x']")
radio_buttons[1].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 group of radio buttons 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 the specified URL, get the radio button with name equal to "fav_subject", and call click() method on the second radio button element of this radio group.

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 buttons with name="fav_subject"
radio_buttons = driver.find_elements(By.XPATH, "//input[@type='radio' and @name='fav_subject']")

# Click the second radio button whose index = 1
radio_buttons[1].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 second radio button

Python Selenium - Screenshot after selecting the second radio button

You can also use JavaScript to select a radio button. For an example on how to select a radio button using JavaScript, please refer the following tutorial.

Python Selenium – Select radio button

Summary

In this Python Selenium tutorial, we have given instructions on how to select the second radio button, with an example.

Related Tutorials

Quiz on Selenium

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

Not answered

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

Not answered

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

Not answered

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

Not answered

Q5. What is the method used to clear the text from an input field in Selenium Python?

Not answered
Code copied to clipboard successfully 👍