Skip to content
CodeKruPro

Read the visible text of elements

Automation taskEasy

"How do you read the text of an element?" The text call returns what a user sees on screen, with extra spaces tidied up. A hidden element gives an empty string, because a user cannot see it. Here you print the text of three elements on one page: plain text, bold text and italic text.

Get Element Text with getText()

Get Element Text with getText()

getText() retrieves the visible text of an element. It works on paragraphs, headings, buttons, spans, etc.

On the Text Messages page, you'll find several text elements:

  • id="plainText" - "A Plain Text"
  • id="boldText" - "A Bold Text" (inside a <strong> tag)
  • id="italicText" - "Italic Text" (inside an <em> tag)

HTML Structure:

<p id="plainText">A Plain Text</p>

<p id="boldText">
  <strong>A Bold Text</strong>
</p>

<p id="italicText">
  <em>Italic Text</em>
</p>

Note: getText() returns visible text including text inside child elements.

Task:

  1. Navigate to https://testkru.com/Elements/TextMessages
  2. Print the text of each element listed above

Expected output:

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