getText() method in Selenium Java with Examples

getText() method is used to get the visible text of an element in Selenium. This post will discuss the getText() method of the WebElement interface in detail.

  • Method declaration – String getText()
  • What does it do? It will get the visible text of an element that is not hidden by the CSS. It also includes the sub-elements of an element.
We have used some keywords like visible text and sub-elements. What do they mean?
Visible Text –

The visible text means the text that is visible on the webpage and not hidden using any CSS property. The getText() method would return an empty string if a text is present for an element but is hidden.

<p>
 This is a text
</p>

Using getText() on the above element will return the “This is a text” string.

<p style="visibility: hidden;">
 This is also a text
</p>

Here, getText() would return an empty string because the element is hidden and cannot be seen on the webpage.

Sub Elements –

An HTML page is made up of web elements, and it follows a tree structure with <body> containing all the elements shown on the web page. So, there is a high chance that an element might also contain other web elements. They can also be looked at as nested elements.

<div id = "parent">
 This is a parent text
  <p id = "child">
     This is a child text
  </p>
</div>
  • Using getText() on the parent element would return both “This is a parent text” and “This is a child text” strings.
  • Using getText() on the child element will return only “This is a child text”.
Code Example

We will use the getText() method on the below-highlighted element, which should return the highlighted text. You can find the highlighted element on this page.

Label field element
  • We will find the element using the id attribute. Here the id of our element is “firstNamePlaceholder“. So, we will use findElement(By.id(“firstNamePlaceholder”)) to get our element.
  • And then, we will use the getText() method on the element.
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test;

public class CodekruTest {

	@Test
	public void test() {

		// pass the path of the chromedriver location in the second argument
		System.setProperty("webdriver.chrome.driver", "C:\\Users\\MEHUL\\OneDrive\\Desktop\\chromedriver.exe");
		WebDriver driver = new ChromeDriver();

		// opening the url
		driver.get("https://testkru.com/Elements/TextFields");

		WebElement element = driver.findElement(By.id("firstNamePlaceholder"));

		System.out.println(element.getText());

	}
}

Output –

1) First Name Without Placeholder

Let’s use the getText() method on an element with several sub-elements. The below image shows one such element.

element containing sub-elements
public class CodekruTest {

	@Test
	public void test() {

		// pass the path of the chromedriver location in the second argument
		System.setProperty("webdriver.chrome.driver", "C:\\Users\\MEHUL\\OneDrive\\Desktop\\chromedriver.exe");
		WebDriver driver = new ChromeDriver();

		// opening the url
		driver.get("https://testkru.com/Elements/TextFields");

		WebElement element = driver.findElement(By.id("textFieldElements"));

		System.out.println(element.getText());

	}
}

Output –

1) First Name Without Placeholder
2) Last Name With Placeholder
3) A textarea field
            
             4) An uneditable field
5) A disabled field
6) Duplicated First Name Field (same as #1)
7) Field with hidden visibility
8) Pre-filled Text Field

We can see that we got the text of all sub-elements as well.

Q – What if there is no text present for an element?

We will get an empty string if no text is present for an element.

Q – What if we use the getText() method on a null WebElement?

We will get a NullPointerException, as illustrated by the below example.

public class CodekruTest {

	@Test
	public void test() {

		// pass the path of the chromedriver location in the second argument
		System.setProperty("webdriver.chrome.driver", "C:\\Users\\MEHUL\\OneDrive\\Desktop\\chromedriver.exe");
		WebDriver driver = new ChromeDriver();

		// opening the url
		driver.get("https://testkru.com/Elements/TextFields");

		WebElement element = null;

		System.out.println(element.getText());

	}
}

Output –

java.lang.NullPointerException: Cannot invoke "org.openqa.selenium.WebElement.getText()" because "element" is null

This is it. Please visit this link to learn more about WebElement and its methods.

We hope that you have liked the article. If you have any doubts or concerns, please write to us in the comments or mail us at admin@codekru.com.

Liked the article? Share this on

Leave a Comment

Your email address will not be published. Required fields are marked *