Skip to content

Make Selenium Easy

And Keep It That Way

  • Home
  • Share
  • Toggle search form

All About getText() method: What, Why And How. | Make Selenium Easy

Posted on 12/05/2018 By admin

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: I Am Inner Text

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);
Output:
Inner text is :India
  • getText() method removes all leading and trailing white spaces.

For example: If the web element is like “

    India    

”, getText() will return only “India”.

  • If you have html as below:

below code will give you same output:

System.out.println(driver.findElement(By.id(“demoLink”)).getText());
System.out.println(driver.findElement(By.id(“demoPara”)).getText());

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:

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;
import org.openqa.selenium.chrome.ChromeDriver;

public class GetTextExample {

        
        public static void main(String[] args) {
                
                System.setProperty("webdriver.chrome.driver","./exefiles/chromedriver.exe");
                WebDriver driver= new ChromeDriver();
                driver.get("https://www.google.co.in/");
                
                List e=driver.findElements(By.xpath("//div[@id='_eEe']/a"));
                System.out.println("Languages supported by Google are:");
                for(WebElement w:e)
                        System.out.println(w.getText());
                
                driver.quit();
        }
}

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.

#HappyLearning

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

Uncategorized

Post navigation

Previous Post: Java Program 08: Program to find occurrence of individual characters in a given string. | Make Selenium Easy
Next Post: Advanced Selenium Concepts – Custom Select Class – Select Value Containing Specific Text in Drop down | Make Selenium Easy

Related Posts

image – Make Selenium Easy Uncategorized
September 15, 2018 – Make Selenium Easy Uncategorized
February 27, 2017 – Make Selenium Easy Uncategorized
May 2019 – Make Selenium Easy Uncategorized
CanNotInject – Make Selenium Easy Uncategorized
indepDep – Make Selenium Easy Uncategorized

Recent Posts

  • Getting Started with Selenium 4: What Is New and How to Upgrade from Selenium 3
  • Manual Testing
  • Baby Steps To Become Efficient Selenium-Java Automation Tester
  • Features of Selenium 4.0.0 Release – Java Binding
  • Part 1: Handling Drop-down Created Using SELECT Tag In Selenium

Recent Comments

No comments to show.

Archives

  • April 2026
  • April 2025
  • March 2025
  • February 2025
  • January 2025
  • December 2024
  • November 2024
  • October 2024
  • September 2024
  • August 2024
  • April 2024
  • March 2024
  • February 2024
  • December 2023
  • October 2023
  • August 2023
  • November 2022
  • September 2022
  • August 2022
  • July 2022
  • May 2022
  • March 2022
  • October 2021
  • April 2021
  • March 2021
  • January 2021
  • December 2020
  • October 2020
  • September 2020
  • August 2020
  • June 2020
  • May 2020
  • April 2020
  • March 2020
  • February 2020
  • January 2020
  • December 2019
  • November 2019
  • October 2019
  • September 2019
  • August 2019
  • May 2019
  • December 2018
  • November 2018
  • October 2018
  • September 2018
  • August 2018
  • July 2018
  • January 2018

Categories

  • Getting Started
  • Uncategorized

Copyright © 2026 Make Selenium Easy.

Powered by PressBook Masonry Dark