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

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

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

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

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.

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

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

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.

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.

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

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.