Make Selenium Easy

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

My name is Amod Mahajan and I am an IT employee with 6+ years of experience in Software testing and staying in Bengaluru. My area of interest is Automation testing. I started from basics and went through so many selenium tutorials. Thanks to Mukesh Otwani as his tutorials are easy and cover basics to advance. I have habit of exploring concepts by deep diving. I used to make notes. I thought of sharing my knowledge through posts and now I am here. #KeepLearning #ShareLearning

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

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.

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

by Amod Mahajan time to read: 2 min
2
%d bloggers like this: