XPath query searching for an element with specific text
Question
Given the following XML structure
<html>
<body>
<div>
<span>Test: Text2</span>
</div>
<div>
<span>Test: Text3</span>
</div>
<div>
<span>Test: Text5</span>
</div>
</body>
</html>
What is the best XPath query to locate any span
with text that starts with Test
?
Solution
//span[starts-with(.,'Test')]
References:
http://www.w3.org/TR/xpath/#function-starts-with
https://developer.mozilla.org/en-US/docs/Web/XPath/Functions/starts-with
OTHER TIPS
Valid option is also:
//span[contains(.,'Test')]
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow