XPath Cheatsheet for Text#
When you need just the content of a node, use the text()
function:
//h1/text()
Exact Matching#
Target all <button>
nodes with a specific text content:
//button[text()='some text']
Get all <p>
nodes that start with placeholder text using the starts-with()
function:
//p[starts-with(text(), 'Lorem ipsum')]
Ensure that all paragraphs end with a dot by getting all <p>
nodes and combining the not()
and ends-with()
functions:
`//p[not(ends-with(text(), '.'))]`
Non-Exact Matching#
Get all <h2>
header nodes that contains some text using the contains()
function:
//h2[contains(text(), 'some text')]
Get all submit buttons using the normalize-space()
function that strips leading and trailing whitespace, including sequences of whitespace characters replaced with a single space. This is useful to target buttons with different text content, yet the same meaning:
//button[normalize-space()='submit']