Skip to content
CodeKruPro

Read Visible Text

22/80

Read Visible Text

.text is the text a person can see. It is not the same as an attribute.

Task:

  1. Open Text Messages
  2. Print the text of #plainText, #boldText, and #italicText

Print the lines below exactly.

Expected output:

Plain: A Plain Text
Bold: A Bold Text
Italic: Italic Text
Read full tutorial on Codekru

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

  1. Load the Text Messages page with the full address given in the task.
  2. Find the plain, bold and italic paragraphs by their ids. Use one locator call per paragraph.
  3. Read the text property of each one. No brackets.
  4. 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 text as a property. Calling it fails with TypeError: 'str' object is not callable. If you come from Java, remember that getText() has no direct Python twin with brackets.

  • Mixing up an id letter case

    Ids are case sensitive. A locator that uses a lowercase t where the page has an uppercase one raises NoSuchElementException: 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.

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.

main.py
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()
Read the full tutorial