All About getText() method: What, Why And How.

Hello Folks,

In this post we will learn below topics:

  1. What is getText() method?
  2. Why we use getText() method?
  3. How we use getText() method?
  4. [Interview Question] Print all languages supported by Google.
  5. What are differences between getText() and getAttribute() methods?

Let’s start:

What is getText() method?

  • getText() is a method which gets you the visible (i.e. not hidden by CSS) innerText of this element, including sub-elements, without any leading or trailing white space.
  • Inner text is text between the opening tags and closing tags.

For example: <a>I Am Inner Text</a>

In above example, texts  “I Am Inner Text” between opening and closing tags are called inner text of web element “a”.

  • We can retrieve innerText of element using getText() method.
  • Return type of getText() method is String.

Why we use getText() method?

  • Maximum links, labels etc in a web page are inner text which are displayed on webpage for end users. So we need to verify weather correct text are displayed or not.
  • It is also useful to locate proper elements while writing automation script based on inner text value.

How to use getText() method?

  • getText() method is used on an element as innerText belongs to an web element.

We will see an example below:

We will retrieve text “India” from Google’s URL. Inner text “India” is in between div tag. So we need to locate div web element.

Code to retrieve inner text:

WebElement textIndiaWebElement= driver.findElement(By.xpath("//div[@class='logo-subtext']"));
String innerText= textIndiaWebElement.getText();
System.out.println("Inner text is :"+innerText);
Inner text is :India
  • getText() method removes all leading and trailing white spaces.

For example: If the web element is like “<p>     India    </p>”, getText() will return only “India”.

  • If you have html as below:

below code will give you same output:



This is inner text.
This is inner text.

  • If you do not have any inner text between tags, getText() method will return null.

Example: <someTag></someTag>

Above html tag has no inner text. So, if we call getText() method on tag, it will give you null.`

Interview question:

Print all language names supported by Google:


package MakeSeleniumEasy;

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;

public class GetTextExample {

	public static void main(String[] args) {
		WebDriver driver= new ChromeDriver();
		List e=driver.findElements(By.xpath("//div[@id='_eEe']/a"));
                System.out.println("Languages supported by Google are:");
		for(WebElement w:e)

Note: If you run above program directly, You will not get correct fonts. It will print “???”.
You need to do below setting:
Right click on program-> Run configuration-> Common-> Encoding-> Others-> Select UTF-8
Apply and run.

Languages supported by Google are:

Difference between getText() and getAttribute() methods in Selenium Webdriver:

I have covered about getAttribute() method in details in this post.
Both methods are used for different purpose.
getAttribute(String attributName) is used to retrieve given attribute value.
getText() is used to retrieve inner text on web element.
Consider below html code:

In above code, input tag has three attributes named “attr1”, “attr2” and “attr3” and also has innerText “foo”.
Consider below codes:

getAttribute(attr1)) will give you "Value1".
getAttribute(attr2)) will give you "Value2".
getAttribute(attr3)) will give you "Value3".
getText()) will give you "foo".

Only scenario when getAttribute() and getText() will return you same value in below html:

For above html code:
getAttribute(“value”) will give you “foo”.
getText() will also give you “foo”.

I hope you must have learnt new things through this post. If you like my posts, please like, comment, share ans subscribe.


Author: Amod Mahajan

My name is Amod Mahajan and I am an IT employee with 4+ years of experience in Software testing and staying in Bengaluru. My area of interest is Automation testing. I started from basics and went throw so many selenium tutorials. Thanks to Mukesh Otwani as his tutorials are easy and cover basics to advance. I have habit of exploring concepts by deep diving. I used to make notes. I thought of sharing my knowledge through posts and now I am here. #KeepLearning #ShareLearning