Skip to content

Make Selenium Easy

And Keep It That Way

  • Home
  • Share
  • About Us
  • Toggle search form

XPath Method- string-length() – Usage in Locating Element

Posted on 02/19/2025 By admin

Hello Guys,

I am going to explain the usage of a method named string-length() from XPath 1.0. This method is very useful to write advanced and dynamic locators.

XPath 1.0 provides an overloaded method named string-length():-

string-length(string) – returns length of given string

string-length() – returns the length of the string specified by the context node.

We can use this method to write xpath for different scenarios. Let’s consider a scenario:-

You search for a product on Amazon and it displays many results. You need to select products whose name has specific count of characters. It may be equals to a number or less than a number or more than a number. For example:- List product names which has a length greater than 5.

Traditional way will be retrieving all names of product and then iterating and filtering as per count of characters in name. Obviously you need to write good more lines of code.

We can use string-length() method to do it at locator level itself. An example is as below:-

I will take a demo website named AutomationPractice. It lists women dresses. I need name of dresses which has more than 8 characters.

Open the developer console and paste below xpath:-

//ul[@class=’product_list grid row’]/li//h5//a[string-length(normalize-space(text()))>8]

I used normalize-space to trim white spaces around text. You can read more about it here.

Using above method you can save a lot of time and unnecessary coding.

package ScenariosPractice; 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 WithoutStringLengthMethod { public static void main(String[] args) { // Set chrome browser executable path System.setProperty("webdriver.chrome.driver", System.getProperty("user.dir") + "/chromedriver.exe"); // Launch chrome WebDriver driver = new ChromeDriver(); // Launch URL driver.get("http://automationpractice.com/index.php?id_category=3&controller=category"); // Locating all names of product List allProductNames= driver.findElements(By.xpath("//ul[@class='product_list grid row']/li//h5//a")); System.out.println("Product names with length more than eight characters:- "); // Iterating the list for(WebElement product: allProductNames) { //Extracting text String productName= product.getText().trim(); // Adding condition to check length if(productName.length()>8) { System.out.println(productName); } } // Closing browser driver.quit(); } } 
package ScenariosPractice; 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 WithStringLengthMethod { public static void main(String[] args) { // Set chrome browser executable path System.setProperty("webdriver.chrome.driver", System.getProperty("user.dir") + "/chromedriver.exe"); // Launch chrome WebDriver driver = new ChromeDriver(); // Launch URL driver.get("http://automationpractice.com/index.php?id_category=3&controller=category"); // Locating all names of product which has length greater than 8 List allProductNames= driver.findElements(By.xpath("//ul[@class='product_list grid row']/li//h5//a[string-length(normalize-space(text()))>8]")); System.out.println("Product names with length more than eight characters:- "); // Iterating the list for(WebElement product: allProductNames) { //Extracting text String productName= product.getText().trim(); System.out.println(productName); } // Closing browser driver.quit(); } } 

Output:-

Product names with length more than eight characters:- 
Faded Short Sleeve T-shirts
Printed Dress
Printed Dress
Printed Summer Dress
Printed Summer Dress
Printed Chiffon Dress

Above is just a scenario. You can use above method in many scenarios as per your need. You can share those scenario in comment to know more people.

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

#ThanksForReading

#ThanksForSharing

Uncategorized

Post navigation

Previous Post: Selenium
Next Post: Javafaker API – Generate Real-Time Fake Data – Faker Class

Related Posts

make selenium easy Uncategorized
Log4j2 Tutorial 6 – Introduction to RollingFileAppender and its Triggers and Strategies Uncategorized
Hierarchy of Selenium Classes and Interfaces – Make Selenium Easy Uncategorized
Launch of Edge and Internet Explorer browser in Selenium 3 Uncategorized
How PageFactory Could Help to Handle StaleElementReferenceException Uncategorized
stream api in selenium Uncategorized

Recent Posts

  • Writing Your First Selenium Test Case: A Complete Beginner Guide
  • Understanding Selenium Architecture and How WebDriver Works Internally
  • How to Set Up Selenium WebDriver with Python Step by Step
  • How to Set Up Selenium WebDriver with Java from Scratch
  • What Is Selenium and Why It Is the Most Popular Test Automation Framework

Recent Comments

No comments to show.

Archives

  • May 2026
  • 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