What if your tests could know exactly when a button is ready without you watching the screen?
Why Checking element state (displayed, enabled, selected) in Selenium Python? - Purpose & Use Cases
Imagine you are testing a website manually and need to check if a button is visible, clickable, or selected before clicking it.
You have to look carefully at the screen, guess if the button is really ready, and try clicking it multiple times to be sure.
This manual way is slow and tiring because you must watch every detail closely.
You might miss if the button is hidden or disabled, causing your test to fail unexpectedly.
It is easy to make mistakes and hard to repeat the same checks exactly every time.
Using Selenium's element state checks, you can ask the computer to tell you if the element is displayed, enabled, or selected.
This removes guesswork and makes your tests faster, more reliable, and repeatable.
if button.is_displayed() and button.is_enabled(): button.click()
if button.is_displayed() and button.is_enabled() and not button.is_selected(): button.click()
This lets you write smart tests that only act when elements are ready, avoiding errors and saving time.
For example, before submitting a form, your test can check if the submit button is visible and enabled, ensuring the form is ready to send.
Manual checks are slow and error-prone.
Element state methods give clear, automatic answers.
Tests become faster, reliable, and easier to maintain.
Practice
Solution
Step 1: Understand the purpose of
This method returns True if the element is visible to the user on the page.is_displayed()Step 2: Compare with other methods
is_enabled()checks if the element can be interacted with,is_selected()checks selection state, andis_visible()is not a valid Selenium method.Final Answer:
is_displayed() -> Option DQuick Check:
Visibility check = is_displayed() [OK]
- Confusing is_enabled() with visibility
- Using is_selected() for visibility
- Assuming is_visible() exists in Selenium
Solution
Step 1: Recall Selenium Python method names
In Selenium Python, the method to check selection isis_selected()with underscores and parentheses.Step 2: Identify incorrect syntax
selected()andselectedare invalid;isSelected()is Java style, not Python.Final Answer:
checkbox.is_selected() -> Option AQuick Check:
Check selected? Use is_selected() [OK]
- Using Java method style isSelected() in Python
- Omitting parentheses after method name
- Using property 'selected' instead of method
button = driver.find_element(By.ID, 'submit') print(button.is_displayed()) print(button.is_enabled())
Solution
Step 1: Understand is_displayed() output
Since the button is visible,is_displayed()returns True.Step 2: Understand is_enabled() output
The button is disabled, sois_enabled()returns False.Final Answer:
True\nFalse -> Option AQuick Check:
Visible = True, Enabled = False [OK]
- Assuming disabled means not visible
- Mixing up is_displayed() and is_enabled() results
- Expecting both to be True for visible button
checkbox = driver.find_element(By.NAME, 'agree')
if checkbox.selected():
print('Selected')
else:
print('Not selected')
What is the error here?Solution
Step 1: Check method name for selection
The correct method to check selection in Selenium Python isis_selected(), notselected().Step 2: Verify other code parts
Parentheses afterfind_elementare correct, locator looks valid, and print statements are properly quoted.Final Answer:
selected() is not a valid method; should use is_selected() -> Option CQuick Check:
Use is_selected() method for checkbox state [OK]
- Using selected() instead of is_selected()
- Confusing method names with Java syntax
- Ignoring method parentheses
button = driver.find_element(By.ID, 'save')
Solution
Step 1: Identify conditions for clicking
The button must be visible (is_displayed()) and enabled (is_enabled()) to be safely clicked.Step 2: Analyze each option
if button.is_displayed() and button.is_enabled(): button.click() uses both conditions with 'and', which is correct. if button.is_selected() and button.is_enabled(): button.click() wrongly usesis_selected()which is for checkboxes/radios. if button.is_displayed() or button.is_enabled(): button.click() uses 'or' which may click if only one condition is true. if button.is_enabled(): button.click() checks only enabled, ignoring visibility.Final Answer:
if button.is_displayed() and button.is_enabled():\n button.click() -> Option BQuick Check:
Click only if visible AND enabled [OK]
- Using or instead of and for conditions
- Checking is_selected() for buttons
- Ignoring visibility check before clicking
Ready to prove it?
Pass the test and this pattern moves unknown
