Introduction
The text property is one of the most commonly used ways to retrieve information from web elements in Selenium. It allows automation engineers to extract the visible text displayed on a webpage and verify whether the application’s behavior matches the expected results.
Text validation plays an important role in automation testing. Whether you’re verifying page headings, error messages, success notifications, product names, or confirmation messages, the text property is extensively used in professional Selenium automation frameworks.
In this tutorial, you’ll learn what the text property is, why it is used, its syntax, practical examples, real-world use cases, common mistakes, and best practices.
What is the text Property?
The text property returns the visible text content displayed by a web element.
Selenium retrieves only the text visible to users on the webpage. Hidden HTML attributes and invisible content are not returned by the text property.
For example, consider the following HTML:
<h1>Welcome to Selenium</h1>
<button>Login</button>
<p>Automation Testing Tutorial</p>
Selenium can retrieve the visible text using:
element.text
The text property is commonly used for retrieving:
Page headings
Error messages
Success notifications
Button labels
Product names
Navigation menu text
Confirmation messages
User-visible content
Why Use the text Property?
The text property is useful because it:
Retrieves visible text from web elements.
Improves automation reliability.
Supports UI validation.
Produces readable test scripts.
Is widely used for assertions.
Is extensively used in professional automation frameworks.
Syntax
element.text
Where:
element→ Previously located WebElement.text→ Returns the visible text displayed by the element.
Example
The Selenium practice website displays a heading on its homepage. Selenium locates the heading element and verifies its visible text using the text property.
The Selenium code is:
from selenium import webdriver
from selenium.webdriver.common.by import By
# Topic: 17. Retrieving Element Information - text Property
# Practice site: https://the-internet.herokuapp.com/
# Run: pytest -s 17_examples/test_01_text_property.py
#
# The text property returns the visible text content of an element.
def test_element_text():
driver = webdriver.Chrome()
try:
driver.get("https://the-internet.herokuapp.com/")
heading = driver.find_element(
By.TAG_NAME,
"h1"
)
assert heading.text == (
"Welcome to the-internet"
)
finally:
driver.quit()
Output
The page heading is
retrieved successfully
using the text property
and matches the expected
value.
Understanding the Code
Import the By Class
from selenium.webdriver.common.by import By
Imports Selenium’s locator strategies.
Open the Practice Website
driver.get(
"https://the-internet.herokuapp.com/"
)
Launches the Selenium practice website.
Locate the Heading
driver.find_element(
By.TAG_NAME,
"h1"
)
Locates the page heading displayed on the webpage.
Retrieve the Visible Text
heading.text
Returns the visible text displayed by the heading element.
Validate the Result
assert heading.text == (
"Welcome to the-internet"
)
Verifies that the retrieved text matches the expected value.
How the text Property Works
Python Script
│
▼
Locate Web Element
│
▼
text Property
│
▼
Retrieve Visible Text
│
▼
Compare Values
│
▼
Perform Validation
Practical Example
Suppose you’re automating an E-Commerce website.
After adding a product to the shopping cart, the webpage displays:
Product added successfully.
Selenium uses:
message.text
to retrieve the success message and verify that the operation completed successfully.
This significantly improves automation reliability during UI validation.
Automation Testing Example
Consider an online banking application.
Automation engineers frequently validate:
Login messages.
Transaction confirmations.
Account balances.
Profile update notifications.
Error messages.
Success alerts.
Professional automation frameworks extensively use the text property during regression testing to verify user-visible content across multiple environments.
Real-World Example
Automation engineers frequently use the text property while automating:
Banking applications.
E-Commerce websites.
Healthcare portals.
CRM systems.
ERP applications.
SaaS products.
Enterprise web applications.
Registration forms.
Dynamic dashboards.
Text validation is one of the most important activities performed during UI automation testing.
Advantages of the text Property
Retrieves visible text efficiently.
Produces readable automation scripts.
Improves automation reliability.
Supports UI validation.
Works across browsers.
Widely used in professional automation frameworks.
Limitations of the text Property
Retrieves only visible text.
Does not return HTML attributes.
Hidden content is ignored.
Dynamic webpages may require synchronization techniques.
Some JavaScript-based UI components may require additional handling.
Common Mistakes Beginners Make
Using text for HTML Attributes
Incorrect
username.text
to retrieve:
<input value="admin">
Correct
username.get_attribute(
"value"
)
The text property retrieves only visible text. It does not return HTML attribute values.
Ignoring Synchronization Issues
Sometimes text is loaded dynamically after the webpage becomes visible.
Always use appropriate synchronization techniques when validating dynamically generated content.
Comparing Incorrect Values
Avoid
assert heading.text == (
"welcome to the-internet"
)
if the actual text is:
Welcome to the-internet
Text comparisons are case-sensitive and must match the expected value accurately.
Best Practices
Use the
textproperty only for visible text validation.Validate user-visible messages whenever appropriate.
Use synchronization techniques for dynamically loaded content.
Use
get_attribute()when retrieving HTML attribute values.Keep assertions simple and readable.
Verify expected values carefully during UI validation.
Conclusion
The text property is one of Selenium’s most important mechanisms for retrieving visible information from web elements. It plays a critical role in validating page content, success messages, headings, and user interactions during automation testing.
Understanding how and when to use the text property correctly is essential for building scalable, reliable, and maintainable Selenium automation frameworks used in professional software testing environments.
Frequently Asked Questions (FAQs)
What is the text property in Selenium?
The text property returns the visible text displayed by a web element.
What is the syntax of the text property?
element.text
Does the text property return HTML attributes?
No.
The text property returns only visible text displayed on the webpage.
How do I retrieve an input field’s value?
Use:
element.get_attribute(
"value"
)
instead of:
element.text
Can the text property fail in Selenium?
Yes.
Incorrect results may occur when:
Content is loaded dynamically.
Hidden elements are used.
Synchronization techniques are not implemented properly.
Incorrect expected values are provided during assertions.
Key Takeaways
The
textproperty retrieves visible text displayed by web elements.It is extensively used for UI validation during automation testing.
Use it for headings, messages, labels, and user-visible content.
Use
get_attribute()when retrieving HTML attribute values.Apply synchronization techniques when working with dynamically loaded text.
The
textproperty is one of the most frequently used Selenium features in professional automation frameworks.
