Skip to content

Make Selenium Easy

And Keep It That Way

  • Home
  • Share
  • Toggle search form

XPath Method – string() – Usage in Locating Element- How it is Different From text()

Posted on 02/19/2025 By admin

Hello Folks,

In this post we are going to learn below topics:-

  1. What is string() method in XPath?
  2. What is difference between string() and text() in XPath?
  3. Usage of string() in XPath.

Before learning about we will understand a term “TextNode“. We can insert text in DOM as a node using TextNode object. TextNode does not contains any tags. It contains only text.

Example:-

This is a simple text in the container.


Above example shows a single node “div” with text node “This is a simple text in the container. You can relate it with usage of text() in XPath. In fact text() is not a function. It is a node test. A node test is a condition on the name, element, attribute, text etc.

You can even locate the text as a node as shown below:-

Look at below html code:-

  Amod Mahajan 28 Make Selenium Easy 

You can say that above html code has multiple text nodes. If you call text() on person node , it will give you all children text nodes as shown below:-

We can get a concatenated text nodes of a context node by using string() method in xpath. If we call string() method on “person” tag, it will give concatenated text node of all children i.e. “Mr. Amod Mahajan 28 Make Selenium Easy” including all whitespaces.

string() method will be very useful in writing xpath. Let’s see some scenarios:-

Suppose you need to locate a person whose age is 28. You can locate using independent dependent concept of XPath as shown below:-

//age[text()=’28’]/..

Same you can do easily with string() method as shown below:-

//person[contains(string(),’28’)]

Consider below html code:-


If you want to locate div with text(), it will not work:-

Try string() method here , it will work fine :-

I think you must have got the difference between string() and text() above. text() is a test node while string() is a method in xpath.

About getText() method of Selenium WebDriver:-

Did you know that getText() method of Selenium WebDriver also returns you the almost same as string() in XPath. I said almost same as getText() truncates whitespaces. As per Selenium documentation:-

Sample Program:-

package ScenariosPractice;

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

public class StringPractice {
        
        public static void main(String[] args) {
                
                System.setProperty("webdriver.chrome.driver", System.getProperty("user.dir") + "/chromedriver.exe");

                WebDriver driver = new ChromeDriver();
                driver.get("file:///C:/Users/amodm/Desktop/stringUsgae.html");
                
                WebElement ele = driver.findElement(By.xpath("//person[contains(string(),'28')]"));
                String text= ele.getText();
                System.out.println(text);
        }

}

Output:-

Amod Mahajan 28 Make Selenium Easy

Hope you learnt something new today and you will be able to utilize this method.

Share this post to your colleagues and friends to extend their knowledge.

#ThanksForReading

#ThanksForSharing

Uncategorized

Post navigation

Previous Post: Git Tutorial 7 – Git Commit – Save Staging Directory Changes To Local Repository
Next Post: Object Repository in Selenium Using Page Factory With Inner Class – Handling a Page Which Has Many Webelements

Related Posts

Jobs Information Uncategorized
February 13, 2018 – Make Selenium Easy Uncategorized
September 7, 2019 – Make Selenium Easy Uncategorized
TestNG Tutorials 51: DataProvider in TestNG – Two Dimensional Array DataProvider Method Uncategorized
ElementClickInterceptedException – Element click intercepted – not clickable at point – Other element would receive the click Uncategorized
Amod Mahajan, Author at Make Selenium Easy – Page 5 of 49 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