About this lesson
Uncheck a checkbox that starts selected, using is_selected in Selenium Python
Selenium Python AssignmentsIntermediateCheckboxes and Radio Buttons
How it works
Selenium has no uncheck() method. A checkbox is a toggle, and the usual action is click(). Whether that click ticks or unticks the box depends on the state it starts in. This is why safe scripts read is_selected() first. It returns a bool: True when the box is checked, False when it is empty.
One box on the Checkboxes page, #firstSelect5, starts checked. You can tell by the checked attribute in the HTML. Asking get_attribute() for it returns the string true on a checked box and None on an unchecked one. That also follows clicks, but is_selected() is the normal check because it returns a real bool.
Because the answers are bools, building a message needs str(). Joining a bare True onto a label raises a TypeError. Convert each value, and keep the capital T and F in mind when you compare against expected text.
About the assignment
Take a checkbox that is ticked when the page opens, show that it is ticked, untick it, and show that it is now empty.
What you will do
- Open the Checkboxes page and locate the pre-ticked checkbox by the id named in the task.
- Read
is_selected()before doing anything else, and print it with its label. Convert the bool to a string. - Click the box one time to toggle it off.
- Read
is_selected()once more from the same variable and print it with the second label.
Common mistakes
Clicking before reading the start state
If you click first and read afterwards, the first line reports the state after the toggle and the label lies. Always read, then act, then read again.
Clicking twice
Two clicks put the box back to checked. The script runs cleanly and the last read says
True, so the symptom is a wrong value with no exception.Printing the bool without str()
A bare bool joined to text with
+fails with aTypeError. This is the most common failure when the first lines of a checkbox script are written.
In a real test suite
Real forms often load with remembered choices, so tests for "turn this off" start from a checked box. A reusable helper reads the state and clicks only when it differs from the wanted one, so the same call works for both start states.
The next lesson looks at radio buttons, where you cannot click a selected one to clear it.
Related lessons
- Select a CheckboxCovers the opposite direction, ticking a box that starts empty.
- Choose One Radio in a GroupShows radios, which cannot be cleared by clicking the selected one.
- Check Whether a Checkbox Is SelectedReads checkbox state as its own check, without any click.
To try one of these calls on any page without a task, open the Online Selenium Python Compiler.
Starter code
The editor above opens with this code. Fill in the TODO lines, then run and submit it.
from selenium import webdriver
from selenium.webdriver.common.by import By
driver = webdriver.Chrome()
try:
driver.get("https://testkru.com/Elements/Checkboxes")
# Clear #firstSelect5 and print both states
finally:
driver.quit()