Skip to content
CodeKruPro

Get Element Text with getText()

19/70

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

About this lesson

getText in Selenium Java: read the visible text of an element

Learn Selenium JavaBeginnerWeb Element Interactions

How it works

getText() returns the text a user can see in an element, including the text of its child elements, as one String. Selenium works it out the way the browser renders it: leading and trailing whitespace is trimmed, and block elements inside the element become separate lines joined by \n.

Visibility matters. The hiddenText paragraph on Text Messages contains text in the HTML but has visibility: hidden, so getText() returns an empty string. To read text that is in the DOM but not shown, read getDomProperty("textContent") instead, and trim it, because textContent keeps the source whitespace.

Whitespace inside the element is preserved only where the browser shows it. The unevenText element is a pre, so the runs of spaces between its words come back exactly as displayed.

About the assignment

Text Messages shows the same kind of text in three wrappers: plain inside a p, bold inside a strong in a p, and italic inside an em in a p. You read all three through their paragraph ids to see that getText() returns the words without the formatting tags.

What you will do

  1. Find each paragraph by its id: plainText, boldText and italicText.
  2. Call getText() on each paragraph, not on the inner strong or em. The child's text is included anyway.
  3. Print three lines with the labels Plain:, Bold: and Italic:, in that order.

Common mistakes

  • Calling getText() on an input

    Inputs keep their text in value, so getText() returns an empty string. Use getAttribute("value"), as in lessons 17 and 18.

  • Expecting hidden text

    An element hidden with CSS gives an empty string, not an exception, so a missing assertion value can look like a page bug. Check isDisplayed() first or read textContent.

  • Comparing against the raw HTML

    The plain paragraph has spaces around its text in the source. getText() trims them, so compare against the trimmed text a user sees, not the markup.

In a real test suite

getText() is behind most content assertions: success and error messages, headings, prices and table cells. Calling it on a large container returns several lines at once, which is handy for logging but brittle for assertions, so target the smallest element that holds the text.

Lesson 18 changed a field's value. The next lesson, 20, reads values that are not visible text at all, the attributes an element carries.

To try one of these calls on any page without a task, open the Online Selenium Java Compiler.

Starter code

The editor above opens with this code. Fill in the TODO lines, then run and submit it.

Main.java
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;

public class Main {
    public static void main(String[] args) throws Exception {
        WebDriver driver = new ChromeDriver();
        driver.get("https://testkru.com/Elements/TextMessages");
        
        // TODO: Get and print text of plainText, boldText, and italicText
        
        driver.quit();
    }
}
Read the full tutorial