How to use a variable in xpath string

Ask general questions here.
kaancha
Posts: 17
Joined: Wed Oct 22, 2014 4:12 pm

How to use a variable in xpath string

Post by kaancha » Tue Jun 02, 2015 9:02 pm

My Object identification is something like this;
/dom[@domain='something.com']//ul[#'bodyForm:messages']/?/?/span[@innertext~'^Hours\ per\ week:\ ''xx''\ is\ n']

How can I replace "XX" with a variable?

Thanks.

krstcs
Posts: 2683
Joined: Tue Feb 07, 2012 4:14 pm
Location: Austin, Texas, USA

Re: How to use a variable in xpath string

Post by krstcs » Tue Jun 02, 2015 9:46 pm

Try this:

Code: Select all

/dom[@domain='something.com']//ul[#'bodyForm:messages']/?/?/span[@innertext~'^Hours\ per\ week:\ ''' + $myVariable + '''\ is\ n']
Notice the single-ticks inside the double-ticks.

Just add <' + $myVariable + '> (without the <> brackets) in place of the XX in your path. XPath strings are single-tick so you break your @innertext match string with those and add the concatenation operator (+) around the variable.
Shortcuts usually aren't...