Skip to content
CodeKruPro

Tell a Disabled Checkbox from an Enabled One

28/80

Tell a Disabled Checkbox from an Enabled One

is_enabled() is false when the control cannot be used. The disabled attribute is a separate fact you can read.

Task:

  1. Print whether #firstSelect1 is enabled
  2. Print whether #firstSelect3 is enabled
  3. Print whether #firstSelect3 has a disabled attribute

Do not use the checkbox whose attribute is misspelled iid. It is not a reliable id.

Print the lines below exactly.

Expected output:

Normal checkbox enabled: True
Disabled checkbox enabled: False
Has disabled attr: True

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

  1. Open the Checkboxes page. Locate the two checkboxes named in the task by their ids, and skip the one with the misspelled attribute.
  2. Read is_enabled() for the normal box and for the disabled one, and print each after its label. Convert each bool.
  3. Read the disabled attribute of the disabled box with get_attribute().
  4. 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") is None. Printing it directly shows None, and joining it fails with a TypeError about NoneType. 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.

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")
    # Compare firstSelect1 and firstSelect3
finally:
    driver.quit()