Skip to content
CodeKruPro

Check Whether a Text Field Is Enabled

36/80

Check Whether a Text Field Is Enabled

is_enabled() on a text field is separate from a checkbox. #disabledField is a disabled input.

Task:

  1. Print whether the first #firstName is enabled
  2. Print whether #disabledField is enabled

Print the lines below exactly.

Expected output:

firstName enabled: True
disabledField enabled: False
Read full tutorial on Codekru

About this lesson

Selenium Python is_enabled: tell a disabled text field from one you can type into

Selenium Python AssignmentsIntermediateElement States

How it works

An input can be on screen and still refuse typing. The disabled attribute does that, and is_enabled() reports it. The method returns a bool: True for a normal field, False when disabled is present.

Enabled and displayed are independent. On the Text Fields page the disabled input is visible, so is_displayed() is True while is_enabled() is False. The hidden input is the reverse: not displayed, yet enabled. Testing one tells you nothing about the other.

Selenium treats the two cases differently when you act. send_keys on a disabled input raises ElementNotInteractableException. A click on it quietly does nothing and returns None, so clicking is a poor way to test for a disabled control.

You can also read get_attribute("disabled"), which gives the string "true" here and None on an enabled field. is_enabled() is cleaner because it returns a real boolean, and that boolean needs str() before you join it to a label.

About the assignment

Compare two text fields on the Text Fields page: the normal first name field and the disabled one. Print whether each is enabled.

What you will do

  1. Find the first firstName field. Two elements share that id, and you want the first.
  2. Find the field called disabledField.
  3. Call is_enabled() on each and turn the answer into text with str().
  4. Print the two lines in that order, each with its own label.

Common mistakes

  • Using the checkbox page by habit

    Earlier lessons checked a disabled checkbox. This one uses a text input on a different page, so ids you remember raise NoSuchElementException.

  • Testing with click or send_keys

    Typing into a disabled input raises ElementNotInteractableException and ends the script before it prints anything. Ask is_enabled() first.

  • Calling the method without brackets

    The bare name is_enabled is a bound method, which counts as true in a condition. A disabled field then looks enabled.

In a real test suite

A real test often checks that a field is disabled until something else happens, such as ticking a terms box. The check runs before and after, and the two answers should differ.

Page objects commonly expose this as a property like is_submit_enabled. The next lesson reads a different state: whether a checkbox is currently ticked.

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/TextFields")
    # Print is_enabled for firstName and disabledField
finally:
    driver.quit()
Read the full tutorial