How to Test For Disabled Elements
Intro
On occasion, you may have the need to check if an element on a page is disabled or enabled. Unfortunately, this is not a well-documented function of Selenium, so doing a trivial action like this can quickly become a pain.
A Solution
If we look at the API documentation for Selenium's IWebElement Interface we can see there is an available
method called Enabled that can help us accomplish what we want.
Let's take a look at how to use it.
Example
For this example we will use a dropdown list from
the-internet. In this list there are a few options to select, one of
which is disabled (Please select an option). Let's find this element and assert that it is disabled.
After loading the page, we find all the elements that have an option tag (which are all the items in the dropdown
list). This returns a collection, so we search it for the item we want based on its text. Once we have the element,
we check whether it's enabled and assert that it isn't. To show the difference, we also check that a regular option
(Option 1) is enabled.
Expected Behavior
- Open a browser
- Visit the page
- Grab all dropdown list elements and find the one we want by its text
- Assert that the element is not enabled
- Close the browser
Summary
Hopefully this tip has helped make the task of seeing whether an element is enabled or disabled more approachable.
Happy Testing!
Authors

