Page 1 of 1

How to use a variable in xpath string

Posted: Tue Jun 02, 2015 9:02 pm
by kaancha
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.

Re: How to use a variable in xpath string

Posted: Tue Jun 02, 2015 9:46 pm
by krstcs
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.