Contents
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 Python 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. Example where Radio button is selected
In this example, we shall consider loading the HTML file at URL: https://pythonexamples.org/tmp/selenium/index-17.html. The contents of this HTML file is given below.
This webpage has a radio group for selecting your favorite 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>
Copy 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 shall surround the code to get radio button in a Python 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
driver = webdriver.Chrome(service=ChromeService(ChromeDriverManager().install()))
driver.set_window_size(500, 400)
# Navigate to the url
driver.get('https://pythonexamples.org/tmp/selenium/index-17.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()
CopyOutput
Radio button is selected.
2. Example where Radio button is not selected
In this example, we shall consider loading the HTML file at URL: https://pythonexamples.org/tmp/selenium/index-16.html. The contents of this HTML file is given below.
In this webpage, radio group is present, 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>
Copy 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
driver = webdriver.Chrome(service=ChromeService(ChromeDriverManager().install()))
driver.set_window_size(500, 400)
# Navigate to the url
driver.get('https://pythonexamples.org/tmp/selenium/index-16.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()
CopyOutput
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.