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
- Open the Checkboxes page and locate the checkbox named in the task by its id. Store it in a variable.
- Call
is_selected()and print the result after the first label. Convert the bool to a string before joining. - Click the checkbox once.
- 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 aTypeErrorthat mentionsbool. Wrap the call withstr().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.
Related lessons
- Clear a Checkbox That Starts SelectedClicks a box that starts checked, so the same click now unchecks it.
- Check Whether a Checkbox Is SelectedChecks whether a checkbox is selected as a standalone state check.
- Choose One Radio in a GroupApplies the same state reads to radio buttons, where clicks behave differently.
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")
# Print #firstSelect1 before and after a click
finally:
driver.quit()