About this lesson
Check a disabled checkbox with is_enabled in Selenium Python
Selenium Python AssignmentsIntermediateCheckboxes and Radio Buttons
How it works
is_enabled() tells you whether a control is disabled or not, which is not the same as being editable. It returns a bool: True for a normal control and False for one that carries the disabled attribute. It is a different question from is_selected().
The disabled attribute is its own fact. get_attribute() returns the string true when it is present and None when it is absent. To print a true or false line, compare the result with the string true and wrap that in str(). A bool joined straight to text raises a TypeError.
Clicking a disabled checkbox is quiet. In a check on this page, click() raised nothing, and is_selected() stayed False. This page also has a checkbox with a misspelled iid attribute. Its id is not usable as a locator, so the task tells you to ignore it.
About the assignment
Contrast a normal checkbox with a disabled one by reading two kinds of information: whether Selenium thinks each can be used, and whether the disabled attribute is present.
What you will do
- Open the Checkboxes page. Locate the two checkboxes named in the task by their ids, and skip the one with the misspelled attribute.
- Read
is_enabled()for the normal box and for the disabled one, and print each after its label. Convert each bool. - Read the
disabledattribute of the disabled box withget_attribute(). - Turn that answer into a true or false statement by comparing it with the text it should hold, then print it as the last line.
Common mistakes
Treating None as the answer
On an enabled box,
get_attribute("disabled")isNone. Printing it directly showsNone, and joining it fails with aTypeErroraboutNoneType. Compare it to the expected string first.Expecting an error from click()
Clicking a disabled checkbox does not raise anything in this run. The box simply stays as it was, so a script that skips the
is_enabled()check can pass silently while doing nothing.Using a locator that does not match
The checkbox with the misspelled attribute cannot be found by that id. A search for it raises
NoSuchElementException: no such element: Unable to locate element, and the message echoes the selector.
In a real test suite
In a real suite, is_enabled() guards form steps: a submit button should be disabled until required fields are filled, and a test asserts both states. A shared helper that returns the state keeps those asserts short.
The next lessons switch to dropdowns, which need the Select helper class.
Related lessons
- Check Whether a Text Field Is EnabledChecks whether a text field is enabled with the same
is_enabled()call. - Select a CheckboxReads checkbox state with
is_selected(), the other state question. - Read an AttributeExplains
get_attribute()and why a missing attribute comes back asNone.
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")
# Compare firstSelect1 and firstSelect3
finally:
driver.quit()