About this lesson
Selenium Python is_selected: read the checkbox state without clicking it
Selenium Python AssignmentsIntermediateElement States
How it works
is_selected() reports whether a checkbox, radio button or dropdown option is currently chosen. It returns a Python bool, and it only reads. Nothing on the page changes, so you can call it as often as you like.
The state comes from the live element, not the HTML file. The checkbox #firstSelect5 has a checked attribute in the markup, so it starts ticked and is_selected() gives True. Its neighbour has no such attribute and gives False. After a click on the neighbour, is_selected() turns True and get_property("checked") agrees, while get_dom_attribute("checked") still gives None because the markup never changed.
That is why is_selected() beats get_dom_attribute(). A click changes the property, not the written attribute. Keep DOM attribute reads for static facts.
The method does not complain about elements that cannot be chosen. On a plain body it returns False, so a wrong locator can hide behind a quiet False. When a result surprises you, check the tag name. And as always, a boolean needs str() before it joins a label.
About the assignment
Open the Checkboxes page and report the starting state of two checkboxes. One begins ticked and the other does not. You only read, so nothing should be clicked.
What you will do
- Locate the checkbox that starts ticked, using its id from the task.
- Locate the checkbox that starts empty.
- Ask each element for
is_selected()and convert the answer withstr(). - Print one line per checkbox, with the ticked one first.
Common mistakes
Clicking before reading
A click toggles the box, so the state you read afterwards is the opposite of the starting state. This task says not to click.
Reading the checked attribute
get_attribute("checked")returns the string"true"for a ticked box andNonefor an empty one. Neither is a bool. Useis_selected().Trusting a False from the wrong element
The page has 21 checkboxes. A made-up id raises
NoSuchElementException, but a different real id just gives a different answer. Check the id carefully.
In a real test suite
Tests use this to confirm defaults and to verify an action, such as "remember me" staying ticked after a reload. A helper that sets a box to a wanted state reads is_selected() first and clicks only when needed.
The next lessons switch from state to timing. Waits are what you use when the state you want is not there yet.
Related lessons
- Select a CheckboxClick a checkbox to select it, which changes the state read here.
- Clear a Checkbox That Starts SelectedClear a checkbox that starts selected, using the state to decide.
- Check Whether a Text Field Is EnabledRead the enabled state of a field, another yes or no question.
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 both checkbox states without clicking
finally:
driver.quit()