Categories: Selenium Topics

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);
Output:
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:

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: <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;
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

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

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

Recent Posts

Ways of Handling StaleElementReferenceException Without PageFactory

Hello Folks, Previously, I had published on Handling StaleElementReferenceException using PageFactory. But many people asked how to handle it if they…

4 days ago

How to Verify if Options in Dropdown are Sorted as Expected In Selenium WebDriver – Java

Hello Guys, As I say always, your automation script is ineffective if you do not include logic to validate to…

2 weeks ago

How Much Java Required For Selenium?

"How much Java I need to learn for selenium with Java binding?" is mostly asked question by a Professional who…

3 weeks ago

How To Verify If An Input Box Accepts Only Numbers Through Selenium

Hello Guys, You should not be able to type alphabets or special characters in a field which supposed to accept…

3 weeks ago

How To Verify Maximum Character Limit of an Input Box Through Selenium

Hello Folks, Recently a guy asked me this question which he was asked in an interview in IBM. What the…

3 weeks ago

API Testing Tutorial Part 15 – Sending GET Request With Params in Postman

Hello Folks, As part of our API Testing series, we will see “Sending GET request with params in Postman”. In last…

3 weeks ago