Introduction
The is_displayed() method is one of Selenium’s most frequently used WebElement methods for verifying whether an element is visible on a webpage. Before performing actions such as clicking buttons, entering text, or validating messages, automation engineers often need to confirm that the required element is actually visible to users.
The is_displayed() method plays an important role in improving automation reliability because interacting with hidden elements can result in unexpected test failures. Professional Selenium automation frameworks extensively use this method while validating page content, forms, buttons, messages, and dynamically loaded UI components.
In this tutorial, you’ll learn what is_displayed() is, why it is used, its syntax, practical examples, real-world use cases, common mistakes, and best practices.
What is is_displayed()?
The is_displayed() method checks whether a web element is currently visible on the webpage.
If the element is visible, Selenium returns:
True
If the element exists in the DOM but is hidden from users, Selenium returns:
False
For example, consider the following HTML:
<input
type="text"
id="username">
<button>
Login
</button>
Selenium can verify whether these elements are visible using:
element.is_displayed()
The is_displayed() method is commonly used for validating:
Textboxes
Buttons
Links
Error messages
Success notifications
Forms
Images
Dynamic UI components
Why Use is_displayed()?
The is_displayed() method is useful because it:
Verifies whether elements are visible to users.
Improves automation reliability.
Prevents interactions with hidden elements.
Supports UI validation.
Produces readable automation scripts.
Is extensively used in professional automation frameworks.
Syntax
element.is_displayed()
Where:
element→ Previously located WebElement.is_displayed()→ ReturnsTrueorFalsebased on the element’s visibility.
Example
The Selenium practice website contains a Username textbox on the Login page. Selenium verifies that the textbox is visible before interacting with it.
The Selenium code is:
from selenium import webdriver
from selenium.webdriver.common.by import By
# Topic: 18. Element State Verification - is_displayed()
# Practice site: https://the-internet.herokuapp.com/login
# Run: pytest -s 18_examples/test_01_is_displayed.py
#
# is_displayed() checks whether an element is visible on the page.
def test_is_displayed():
driver = webdriver.Chrome()
try:
driver.get(
"https://the-internet.herokuapp.com/login"
)
username = driver.find_element(
By.ID,
"username"
)
assert username.is_displayed()
finally:
driver.quit()
Output
The Username textbox is
visible on the webpage,
and Selenium successfully
validates its visibility
using is_displayed().
Understanding the Code
Import the By Class
from selenium.webdriver.common.by import By
Imports Selenium’s locator strategies.
Open the Login Page
driver.get(
"https://the-internet.herokuapp.com/login"
)
Launches the Selenium practice website.
Locate the Username Field
driver.find_element(
By.ID,
"username"
)
Locates the Username textbox displayed on the webpage.
Verify Visibility
username.is_displayed()
Returns:
True
which confirms that the element is visible to users.
Validate the Result
assert username.is_displayed()
Verifies that Selenium successfully confirms the visibility of the Username textbox.
How is_displayed() Works
Python Script
│
▼
Locate Web Element
│
▼
is_displayed()
│
▼
Check Element Visibility
│
▼
Return Result
True/False
│
▼
Perform Validation
Practical Example
Suppose you’re automating an E-Commerce website.
After successfully placing an order, the webpage displays:
Order Placed Successfully
Selenium can verify whether the success message is visible using:
message.is_displayed()
before validating its contents.
This significantly improves automation reliability when testing user workflows.
Automation Testing Example
Consider an online banking application.
Automation engineers frequently validate whether the following elements are visible:
Login forms.
Transaction details.
Success messages.
Error notifications.
Profile information.
Navigation menus.
Payment confirmation dialogs.
Dynamic UI components.
Professional automation frameworks extensively use is_displayed() during UI regression testing.
Real-World Example
Automation engineers frequently use is_displayed() while automating:
Banking applications.
E-Commerce websites.
Healthcare portals.
CRM systems.
ERP applications.
SaaS products.
Enterprise web applications.
Registration forms.
Dynamic dashboards.
Visibility validation has become increasingly important because modern applications dynamically show and hide elements using JavaScript and CSS.
Advantages of is_displayed()
Improves automation reliability.
Prevents interactions with hidden elements.
Supports UI validation.
Produces maintainable automation scripts.
Works across browsers.
Extensively used in professional automation frameworks.
Common Mistakes Beginners Make
Performing Actions Without Checking Visibility
Avoid
username.click()
without verifying whether the element is visible.
Prefer
if username.is_displayed():
username.click()
Visibility validation significantly improves automation reliability.
Assuming Element Presence Means Visibility
An element may:
Exist in the DOM
│
▼
Hidden
│
▼
Not Visible to Users
In such cases:
is_displayed()
returns:
False
Always validate visibility whenever appropriate.
Ignoring Dynamic Webpages
Modern web applications frequently:
Hide elements.
Display popups dynamically.
Render components asynchronously.
Always use appropriate synchronization techniques before validating visibility.
Best Practices
Verify element visibility before performing critical actions.
Use
is_displayed()when validating success and error messages.Apply synchronization techniques for dynamically loaded webpages.
Keep assertions simple and readable.
Combine visibility validation with other WebElement methods whenever appropriate.
Validate UI components before interacting with them.
Conclusion
The is_displayed() method provides a simple and reliable mechanism for verifying whether web elements are visible during Selenium automation testing. It plays an important role in improving automation reliability, validating user interfaces, and preventing interactions with hidden elements.
Understanding how and when to use is_displayed() correctly is essential for building scalable, reliable, and maintainable Selenium automation frameworks used in professional software testing environments.
Frequently Asked Questions (FAQs)
What is is_displayed() in Selenium?
The is_displayed() method checks whether a web element is visible on the webpage.
What is the syntax of is_displayed()?
element.is_displayed()
What does is_displayed() return?
It returns:
True
if the element is visible and:
False
if the element is hidden.
Does is_displayed() verify whether an element exists?
No.
The element must first be successfully located. The is_displayed() method only checks whether it is visible.
When should I use is_displayed()?
Use it when:
Validating page elements.
Verifying success or error messages.
Testing dynamically displayed components.
Performing actions only on visible elements.
Key Takeaways
is_displayed()verifies whether a web element is visible on the webpage.It returns
Truefor visible elements andFalsefor hidden elements.Use it before interacting with important UI components whenever appropriate.
Visibility validation significantly improves automation reliability.
Apply synchronization techniques when working with dynamic webpages.
The
is_displayed()method is extensively used in professional Selenium automation frameworks.
