Given that the HTML contains:
<div tagname="779853cd-355b-4242-8399-dc15f95b3276_Destination" class="panel panel-default"></div>
How do we write the following expression in XPath:
Find a <div>
element whose tagname
attribute ends with the string 'Destination'
I've been searching for days and I can't come up with something that works. Among many, I tried for example:
div[contains(@tagname, 'Destination')]
XPath 2 or 3: There's always regex.
You can use
ends-with
(Xpath 2.0)You could use the below xpath which will work with Xpath 1.0
//div[string-length(substring-before(@tagname, 'Destination')) >= 0 and string-length(substring-after(@tagname, 'Destination')) = 0 and contains(@tagname, 'Destination')]
Basically it checks if there is any string ( or no strings ) before the first occurrence of
Destination
but there should not be any text after theDestination
Test input :
Test output:
XPath 2.0
XPath 1.0