Learn About Less Talked & Used XPath Function – concat()

The concat function concatenates two or more strings and returns the resulting string.

Syntax :- concat(string1 ,string2 [,stringn]* )

Real Time Usage

This can be used to construct custom XPath. For an example :- When we register a user , a hyperlink is shown which concatenates User’s First Name and LastName followed by City user stays. Something as below:-

Amod Mahajan Bengaluru

We can use concat() function to write custom XPath as below:-

String FirstName = "";
String LastName = "";
String City = "";
String xpath = "//a[text()=concat('"+FirstName+" ','"+LastName+" ','"+City+" ')]";

But you don’t need to use concat() function as you can achieve the same using + sign as below :-

String FirstName = "";
String LastName = "";
String City = "";
String xpath = "//a[text()='"+FirstName+" "+LastName+" "+City+"']";

Have you encountered an attribute value or inner text containing double quotes and single quotes both? Something as below:-

Hello "Amod". How is Rahul's pet?

Normal way of writing XPath will not work because of enclosing quotes and you can not escape it. If we keep the text within single quotes or double quotes, we get another within text which can not be escaped.

//p[text()='Hello "Amod". How is Rahul's pet?']
//p[text()="Hello "Amod". How is Rahul's pet?"]

If you directly use in code, it will give you invalid selector.

String text2= driver.findElement(By.xpath("//p[text()=\"Hello \"Amod\". How is Rahul's pet?\"]")).getText();
System.out.println(text2);
Exception in thread "main" org.openqa.selenium.InvalidSelectorException: invalid selector: 
Unable to locate an element with the xpath expression //p[text()="Hello "Amod". How is Rahul's pet?"] because of the following error:
SyntaxError: Failed to execute 'evaluate' on 'Document': The string '//p[text()="Hello "Amod". How is Rahul's pet?"]' is not a valid XPath expression.

In above scenario, contains method will work.

//p[text()=concat(‘Hello “Amod”. How is Rahul’,”‘s pet?”)]

I enclosed part of text containing double quotes in single quotes and part of text containing single quotes within double quotes and concatenate.

Program

package ScenarioBased;

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

import io.github.bonigarcia.wdm.WebDriverManager;

public class ContainsXPathMethod {
	
	public static void main(String[] args) {
		
		// Browser initialization
		WebDriverManager.chromedriver().setup();
		WebDriver driver = new ChromeDriver();
		String fileURL = System.getProperty("user.dir");
		driver.get(fileURL + "/src/test/resources/htmlFiles/WithDoubleSIngleText.html");
		
		
		String text1= driver.findElement(By.xpath("//p[text()=concat('Hello \"Amod\". How is Rahul',\"'s pet?\")]")).getText();
		System.out.println(text1);
		
//		String text2= driver.findElement(By.xpath("//p[text()=\"Hello \"Amod\". How is Rahul's pet?\"]")).getText();
//		System.out.println(text2);
		
		
	}

}

Output

You can clone code from my git repo.

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

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.

3 thoughts on “Learn About Less Talked & Used XPath Function – concat()”

  1. Hello Amod. Great info. Can you please explain logic behind this. Why you enclosed text containing single quotes within double qoutes and vice versa

Leave a Reply

Your email address will not be published. Required fields are marked *