Selenium Python – Check if radio button is selected
In this tutorial, you will learn how to check if a radio button is selected in a group of related radio buttons using Selenium in Python.
To check if a radio button is selected in Selenium Python, find the radio button in the group which has been checked using XPath expression using find_element()
method. If the method throws NoSuchElementException, then we can say that no radio button in the group has been selected. We can use try-except around find_element()
method.
In the following code snippet, we use try-except to check if any radio button is selected in the group of radio buttons with the name='radio-group-name'
is
try:
driver.find_element(By.XPATH, "//input[@type='radio' and @name='radio-group-name' and @checked]")
print("Radio button is selected.")
except NoSuchElementException:
print("Radio button is not selected.")
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")
Examples
1. Radio button is selected
Consider the following HTML file index.html that has a radio group for selecting your favourite subject, and the radio button with value="Mathematics"
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>

In the following program, we initialize a webdriver, navigate to a specific URL (index.html) that is running on a local server, and get the radio button with name equal to "fav_subject"
and attribute checked. We surround the code to get radio button in a try-except statement to check if a radio button is selected or not. We shall print the result to standard 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
from selenium.common.exceptions import NoSuchElementException
# 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('http://127.0.0.1:5500/index.html')
# Get the selected radio button
try:
radio_button = driver.find_element(By.XPATH, "//input[@type='radio' and @name='fav_subject' and @checked]")
print("Radio button is selected.")
except NoSuchElementException:
# Exception occurs if there is no selected radio button
print("Radio button is not selected.")
# Close the driver
driver.quit()
Output
Radio button is selected.
2. Radio button is not selected
Consider the following HTML file index.html that has a radio group for selecting your favourite subject, and no radio button is selected.
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">
<label for="mathematics">Mathematics</label><br>
<input type="radio" id="chemistry" name="fav_subject" value="Chemistry">
<label for="chemistry">Chemistry</label>
</form>
</body>
</html>

Run the same program we have given in the previous example.
Output
Radio button is not selected.
Summary
In this Python Selenium tutorial, we have given instructions on how to check if a radio button is selected or not, with example programs.