XPath testing that string ends with substring?

Happy Bird picture Happy Bird · Dec 2, 2016 · Viewed 22.3k times · Source

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')]

Answer

kjhughes picture kjhughes · Dec 2, 2016

XPath 2.0

//div[ends-with(@tagname, 'Destination')]

XPath 1.0

//div[substring(@tagname, string-length(@tagname) 
                          - string-length('Destination') + 1)  = 'Destination']