Using getText() method in Selenium WebDriver

Interface WebElement contains a method “getText()” whose return type is a String. If you refer official document of Selenium then you can find a clear crystal sentence stating what this method does :-

Get the visible (i.e. not hidden by CSS) innerText of this element, including sub-elements.

I did not understand the terms such as “not hidden by CSS“, “innerText“, “including sub-elements“.

Let’s forget getText() method for some time and research more on words quoted above.

What is innerText?

Observe the rendered text between opening and closing nodes above. For example – “This is a paragraph” between opening node <p> and closing node </p> is an innerText. Similarly “Logout” and “Hello Amod” are innerTexts of button and span respectively.

In above example, “Sign In” is innerText of first “button” node while “Sign Up” is the innerText for second “button” and “Sign In Sign Up” is innerText of “p” node.

Now read the definition of getText() method again or observe bold text in below paragraph.

Get the visible (i.e. not hidden by CSS) innerText of this element, including sub-elements.

Is it making sense? Inner text of a node combines rendered text of all its children node as it appears. If I say that give me innerText of node first “button” then it will see there is no child node and returns only “Sign In”. Same case for second button tag. But if I ask you to give innerText of node “p” then it will see that “p” node has two children node and it will combine text from both nodes and return i.e. “Sign In Sign Up”.

What will be innerText for each node above? It will be :-

innerText of node p will be innerText of first button node + innerText of second button node + rendered text of node p.

Let’s not believe on my words. Let’s do some practicals.

Let’s create above shown html with ids as below:-

Now learn a new thing that “innerText” is a property of html element. That is the reason I used exactly “innerText” everywhere above and same in getText() description by Selenium WebDriver developer. So the innerText is the property of the HTMLElement interface represents the “rendered” text content of a node and its descendants.

Now write simple Javascript commands in Chrome console and observe output and find that whatever I said is correct.

We just learnt that innerText of node is combined rendered text of all children nodes. But there is a twist. It ignores rendered text of <script> and <style> tags. It also ignores the text hidden by CSS. Let’s see an example below :-

In above example there are <style> and <script> tags and also a span tag which is hidden using “display:none”. innerText will skip style and script tags and also span tag as it is hidden.

Hopefully you understand the term “not hidden by CSS” in description of getText().

Let’s write some Selenium codes :-


Let’s learn extra.

What getText() method will return for a node which has no innerText?

Answer is an empty string i.e. a string with length Zero.

Sample html code

Selenium Code


Will getText() method trim leading , trailing or white spaces in between? Will it give innerText which has multiple lines or <br>?

Answer is as it looks or appears on browser. I will show you an example where white spaces were provided normally and also using &nbsp.

Html Code

On browser it looks like as below. Notice normal white spaces are trimmed by default but non breakable spaces remain as it is.

getText() method will also return in the same way.


Interview question

Print all language names supported by Google.

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

You can download/clone above sample project from here.

If you have any doubt, feel free to comment below.
If you like my posts, please like, comment, share and subscribe.

Find all Selenium related post here, all API manual and automation related posts here and find frequently asked Java Programs here.

Many other topics you can navigate through menu.

Author: Amod Mahajan

A software Tester who is paid to judge products developed by others. Writing technical posts and creating YouTube videos are my hobbies.

6 thoughts on “Using getText() method in Selenium WebDriver

    1. when i enter the getText command, selenium is unable to identify it. please assist me and let me know to workaround this and wether i need to import any additional packages to activate it

Leave a Reply

Please wait...

Subscribe to new posts to become automation expert

Want to be notified when my new post is published? Get my posts in your inbox.