Skip to content
CodeKruPro

Select a Checkbox

25/80

Select a Checkbox

is_selected() reads whether a checkbox is checked. click() changes it. Python prints True and False.

Task:

  1. Open Checkboxes
  2. Print whether #firstSelect1 is selected
  3. Click it and print the state again

Print the lines below exactly.

Expected output:

Before click: False
After click: True

About this lesson

Click a checkbox and check it with is_selected in Selenium Python

Selenium Python AssignmentsIntermediateCheckboxes and Radio Buttons

How it works

A checkbox is an input whose type is checkbox. Its state is either checked or not, and Selenium reports it through is_selected(). The call returns a Python bool, so the answer is True or False, never a string. A single click() toggles the box, and a second click puts it back, which is how you test both directions.

The bool is where Python bites. Joining it to a label with + raises TypeError with the message can only concatenate str (not "bool") to str. You need str() around the call first, and then the text reads True or False with a capital letter. The brackets matter too. Leaving them off gives you the bound method, which is always truthy, so a check like if box.is_selected: passes even when the box is empty.

Read the state before you click, not only after. That way the script proves what changed, instead of assuming the starting point.

About the assignment

Show that clicking an unchecked checkbox changes it. You report the state before the click and the state after it, for one specific checkbox.

What you will do

  1. Open the Checkboxes page and locate the checkbox named in the task by its id. Store it in a variable.
  2. Call is_selected() and print the result after the first label. Convert the bool to a string before joining.
  3. Click the checkbox once.
  4. Call is_selected() again on the same variable and print the result after the second label.

Common mistakes

  • Joining a bool to a string

    A line that adds box.is_selected() straight onto a label raises a TypeError that mentions bool. Wrap the call with str().

  • Forgetting the brackets on is_selected

    Without brackets you hold a method object, not an answer. Printing it shows something like <bound method WebElement.is_selected ...>, and it is truthy in any condition.

  • Clicking a box that is already checked

    A click toggles, so clicking a box that starts checked unchecks it. Read the state first. If you only want the box to end up checked, click only when is_selected() is false.

In a real test suite

Teams wrap this in a helper such as set_checked(box, wanted) that reads is_selected() and clicks only when the state differs. Tests then say what they want, and they do not break when a default changes.

The next lesson uses the same calls on a box that starts checked, so the click works in the other direction.

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")
    # Print #firstSelect1 before and after a click
finally:
    driver.quit()