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
- Find the first
firstNamefield. Two elements share that id, and you want the first. - Find the field called
disabledField. - Call
is_enabled()on each and turn the answer into text withstr(). - 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
ElementNotInteractableExceptionand ends the script before it prints anything. Askis_enabled()first.Calling the method without brackets
The bare name
is_enabledis 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.
Related lessons
- Tell a Disabled Checkbox from an Enabled OneTell disabled and enabled checkboxes apart, the same idea on another control.
- Check Whether an Element Is DisplayedCheck visibility, which is a separate state from being enabled.
- Type into a Text FieldType into an enabled text field, the action a disabled one refuses.
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/TextFields")
# Print is_enabled for firstName and disabledField
finally:
driver.quit()