Skip to content
CodeKruPro

Clear a Checkbox That Starts Selected

26/80

Clear a Checkbox That Starts Selected

A checkbox that starts selected is cleared by clicking it again.

Task:

  1. Print whether #firstSelect5 is selected
  2. Click it
  3. Print the state again

Print the lines below exactly.

Expected output:

Initially selected: True
After deselect: False

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

  1. Open the Checkboxes page and locate the pre-ticked checkbox by the id named in the task.
  2. Read is_selected() before doing anything else, and print it with its label. Convert the bool to a string.
  3. Click the box one time to toggle it off.
  4. 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 a TypeError. 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.

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.

main.py
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()