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
- Find each paragraph by its id:
plainText,boldTextanditalicText. - Call
getText()on each paragraph, not on the innerstrongorem. The child's text is included anyway. - Print three lines with the labels
Plain:,Bold:andItalic:, in that order.
Common mistakes
Calling getText() on an input
Inputs keep their text in
value, sogetText()returns an empty string. UsegetAttribute("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 readtextContent.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.
Related lessons
- XPath Axes - Parent and SiblingThe bold paragraph from this lesson, reached through XPath axes.
- Get Attribute Value with getAttribute()Read
value,hrefand other attributes thatgetText()does not return. - isDisplayed() - Check Element VisibilityCheck
isDisplayed()before trusting an emptygetText()result.
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.
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();
}
}