About this lesson
Get visible text with element.text in Selenium Python: read three paragraphs
Selenium Python AssignmentsBeginnerWeb Element Interactions
How it works
In Selenium Python, text is a property of a web element, not a method. You write it without round brackets. It returns a plain str holding what a person looking at the screen would read inside that element. Adding brackets turns it into a call on a string and raises TypeError: 'str' object is not callable.
Selenium builds that string from the rendered page, not from the HTML source. Tags are stripped, so words wrapped in strong or em still come back as one clean sentence. Text that is hidden with CSS is left out, and you get an empty string. The hidden paragraph on this practice page shows that: it has content in the HTML, yet text is empty and is_displayed() is false.
That makes text different from an attribute. An attribute is something written inside the opening tag, such as id or value. Visible text sits between the tags. Since text is already a string, putting it after a label with + just works.
About the assignment
Open the Text Messages page and report the visible words of three paragraphs. Each paragraph has a different style, so you also see that formatting tags do not change what text returns.
What you will do
- Load the Text Messages page with the full address given in the task.
- Find the plain, bold and italic paragraphs by their ids. Use one locator call per paragraph.
- Read the
textproperty of each one. No brackets. - Print one line for each, in the order plain, bold, italic, with its label first.
Common mistakes
Writing .text() like a method
Selenium Python exposes
textas a property. Calling it fails withTypeError: 'str' object is not callable. If you come from Java, remember thatgetText()has no direct Python twin with brackets.Mixing up an id letter case
Ids are case sensitive. A locator that uses a lowercase
twhere the page has an uppercase one raisesNoSuchElementException: no such element: Unable to locate element, with the selector echoed in the message. Copy the id exactly.Expecting text from a hidden element
A paragraph hidden by CSS gives an empty string, not an error. If a line prints blank, check
is_displayed()before blaming the locator.
In a real test suite
In a page object, text reads become small methods like get_banner_message() that return the string. Tests then compare it against an expected message, and a failed comparison shows both strings in the pytest report.
The next lesson reads attributes, the other half of the information an element carries.
Related lessons
- Click a Button and Read the New TextReads text after a click changes it, a common use of
.text. - Read an AttributeReads attributes instead of visible text, so you can compare the two ideas.
- Check Whether an Element Is DisplayedChecks
is_displayed(), which explains why hidden text comes back empty.
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/TextMessages")
# Print the visible text of the three messages
finally:
driver.quit()