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 code

  • 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.


In this example, we shall consider loading the webpage at URL: The contents of this HTML file is given below.

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

The webpage has a radio group with radio button value="Mathematics" that has already been checked in the HTML using checked attribute.

Selenium Python - Uncheck the selected radio button

In the following program, we initialize a webdriver, navigate to a specified URL, and get the radio button with name equal to "fav_subject" and attribute checked. We shall uncheck this radio button by running JavaScript via Selenium.

Python Program

from selenium import webdriver
from import ChromeDriverManager
from import Service as ChromeService
from import By
import time

# Setup chrome driver
driver = webdriver.Chrome(service=ChromeService(ChromeDriverManager().install()))

# Navigate to the url

# Get the selected radio button
radio_button = driver.find_element(By.XPATH, "//input[@type='radio' and @name='fav_subject' and @checked]")


# Run custom JavaScript to uncheck the radio button
driver.execute_script("arguments[0].checked = false;", radio_button)


# Close the driver

time.sleep() statements are introduced to have a delay, so that we can see Selenium unchecking the radio button. Otherwise, it would be too fast, and we may think that the radio button is already not checked.

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 using Selenium.

Selenium Python - After unchecking the selected radio button


In this Python Selenium tutorial, we have given instructions on how to uncheck the selected radio button using Selenium via 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 navigate back to the previous page in Selenium Python?

Not answered

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

Not answered

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

Not answered
Code copied to clipboard successfully 👍