How to uncheck radio button in Selenium Python?

Selenium Python – Uncheck the radio button

In this tutorial, you will learn how to unselect or uncheck a radio button in a group of related radio buttons using Selenium in Python.

To unselect a radio button in Selenium Python, find the radio button in the group which has been checked using XPath expression. Once you get the selected radio button, you can uncheck by running JavaScript on that element to set the checked property to false using execute_script() method.

In the following code snippet, we get the selected radio button from a group with name="radio-group-name", and uncheck it using JavaScript.

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

In the above XPath expression

  • Selected radio_button is passed as second argument to execute_script(), which is then internally passed as argument to the JavaScript code.
  • arguments[0] gets the radio button.
  • arguments[0].checked = false; unchecks the radio button.

Example

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>
Selenium Python - Uncheck the selected radio button

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 uncheck this radio button using JavaScript.

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)

# Navigate to the url
driver.get('http://127.0.0.1:5500/index.html')

# Get the seleradio 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()

screenshot-1.png – Before unchecking the selected radio button.

Selenium Python - Before unchecking the selected radio button

screenshot-2.png – After unchecking the selected radio button.

Selenium Python - After unchecking the selected radio button

Summary

In this Python Selenium tutorial, we have given instructions on how to uncheck the selected radio button using JavaScript, 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 method to wait for a web element to load in Selenium Python?

Not answered

Q3. What is the method used to switch to a different frame in Selenium Python?

Not answered

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

Not answered

Q5. Which of the following is not a commonly used web element locator in Selenium Python?

Not answered