Contents
Selenium Python – Unselect a checkbox
In this tutorial, you will learn how to unselect or uncheck a checkbox in a form, in Selenium Python.
To unselect a checkbox in Selenium Python, find the checkbox WebElement, check if the checkbox is selected, and if selected, call click() method of the checkbox object to unselect it. This click action makes the checkbox unselected or unchecked.
The method click() makes a click on the checkbox element, and since we have already checked if is selected, the checkbox should be unselected.
Examples
In this example, we shall consider loading the webpage at URL: https://pythonexamples.org/tmp/selenium/index-22.html. The contents of this webpage is given below. The webpage contains three checkboxes, where the last checkbox is already checked via HTML script.
<html>
<body>
<h1>My Sample Form</h1>
<form id="myform">
<input type="checkbox" id="apple" name="apple" value="Apple">
<label for="apple"> I eat apples.</label><br>
<input type="checkbox" id="banana" name="banana" value="Banana">
<label for="banana"> I eat bananas.</label><br>
<input type="checkbox" id="cherry" name="cherry" value="Cherry" checked>
<label for="cherry"> I eat cherries.</label><br>
</form>
</body>
</html>
Copy ![Python Selenium - Select a Checkbox - Before selection screenshot](https://pythonexamples.org/wp-content/uploads/2023/04/screenshot-1-6.webp)
The first two checkboxes are not checked and the last checkbox is checked.
1. Unselect a Checkbox
In this example, the checkbox we are interested is the one with the id "cb3".
In the following program, we initialize a webdriver, load specified URL, get the checkbox with id attribute is equal to "cherry", and call click() method on this checkbox element.
We shall take screenshots before and after unselecting the checkbox.
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
driver = webdriver.Chrome(service=ChromeService(executable_path=ChromeDriverManager().install()))
driver.set_window_size(500, 400)
# Navigate to the url
driver.get('https://pythonexamples.org/tmp/selenium/index-22.html')
# Find checkbox element
mycheckbox = driver.find_element(By.ID, 'cherry')
# Take a screenshot
driver.save_screenshot("screenshot-1.png")
# Select checkbox if not already selected
if mycheckbox.is_selected():
mycheckbox.click()
print(f"{mycheckbox.get_attribute('value')} checkbox is unselected.")
else:
print(f"{mycheckbox.get_attribute('value')} checkbox is already unselected.")
# Take a screenshot
driver.save_screenshot("screenshot-2.png")
# Close the driver
driver.quit()
CopyOutput
Cherry checkbox is unselected.
screenshot-1.png – Before unselecting the Cherry checkbox
![Python Selenium - Unselect a Checkbox - Before screenshot](https://pythonexamples.org/wp-content/uploads/2023/04/screenshot-1-6.webp)
screenshot-2.png – After unselecting the Cherry checkbox
![Python Selenium - Unselect a Checkbox - After screenshot](https://pythonexamples.org/wp-content/uploads/2023/04/screenshot-2-6.webp)
2. Unselect a Checkbox that is already unselected
In this example, the checkbox we are interested is the one with the id "cb1" which is already unselected or unchecked.
In the following program, we initialize a webdriver, load specified URL, get the checkbox with id attribute is equal to "apple", and call click() method on this checkbox element only if this checkbox is not already unselected.
We are checking if the checkbox is selected using is_selected() method of the checkbox object.
We shall take screenshots before and after unselecting the checkbox.
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
driver = webdriver.Chrome(service=ChromeService(executable_path=ChromeDriverManager().install()))
driver.set_window_size(500, 400)
# Navigate to the url
driver.get('https://pythonexamples.org/tmp/selenium/index-22.html')
# Find checkbox element
mycheckbox = driver.find_element(By.ID, 'apple')
# Take a screenshot
driver.save_screenshot("screenshot-1.png")
# Select checkbox if not already selected
if mycheckbox.is_selected():
mycheckbox.click()
print(f"{mycheckbox.get_attribute('value')} checkbox is unselected.")
else:
print(f"{mycheckbox.get_attribute('value')} checkbox is already unselected.")
# Take a screenshot
driver.save_screenshot("screenshot-2.png")
# Close the driver
driver.quit()
CopyOutput
Cherry checkbox is already selected.
screenshot-1.png – Before unselecting the Apple checkbox
![Python Selenium - Unselect a Checkbox that is already selected - Before screenshot](https://pythonexamples.org/wp-content/uploads/2023/04/screenshot-1-6.webp)
screenshot-2.png – After unselecting the Apple checkbox
![Python Selenium - Unselect a Checkbox that is already selected - After screenshot](https://pythonexamples.org/wp-content/uploads/2023/04/screenshot-1-6.webp)
Summary
In this Python Selenium tutorial, we have given instructions on how to unselect a checkbox using click() method of the checkbox WebElement object, using get_attribute() method.