XPath Functions Reference (with Examples)
Every XPath function you need, in one place. Browse string, numeric, boolean, and node helpers with Selenium-friendly examples. Works great with the XPath playground for quick validation.
String functions
contains(haystack, needle)
contains(@class, 'btn')
Partial match for attributes or text nodes.
starts-with(haystack, prefix)
starts-with(@id, 'user-')
Great for dynamic IDs.
substring(string, start, length)
substring(text(), 1, 5)
Positions start at 1 in XPath.
substring-before(string, delimiter)
substring-before(@href, '?')
Use for URL base extraction.
substring-after(string, delimiter)
substring-after(@href, '=')
Capture query parameter values.
string-length(string)
string-length(text()) > 10
Guard against empty nodes.
concat(str1, str2, ...)
concat(@first, '-', @last)
Combine attributes for unique keys.
normalize-space(string)
normalize-space(text())
Trim and collapse whitespace.
translate(string, from, to)
translate(@class,'ABC','abc')
Normalize casing.
Numeric functions
count(node-set)
count(//div)
Returns number of nodes.
sum(node-set)
sum(//price/@amount)
Sum numeric attributes.
number(value)
number(@value)
Cast strings to numbers.
floor(number)
floor(3.9)
Round down.
ceiling(number)
ceiling(3.1)
Round up.
round(number)
round(3.5)
Banker’s rounding in XPath 1.0.
Boolean functions
boolean(object)
boolean(@checked)
Convert to boolean.
not(boolean)
not(@disabled)
Negate conditions.
true()
true()
Return true literal.
false()
false()
Return false literal.
Node functions
name(node)
name() = 'div'
Full QName.
local-name(node)
local-name() = 'svg'
Without namespace prefix.
namespace-uri(node)
namespace-uri()
Useful in SVG/XML docs.
position()
position() = 1
Index within the current context.
last()
//li[last()]
Last node in the context set.
Function usage tips
- String comparison is case-sensitive; normalize with
translate()if needed. - Use
number()before math comparisons on numeric-looking attributes. - Combine
count()with predicates to enforce structure, e.g.,//form[count(input)>=2]. position()is relative to the current node set—scope it with a leading dot when chaining.
Browser compatibility
- document.evaluate (XPath 1.0) is supported in Chrome, Firefox, Safari, Edge.
- XPath 2.0 functions (e.g., matches()) are not available in native browsers—use a library such as fontoxpath.
- SVG and XML namespaces require
namespace-uri()or explicit prefixes. - Playwright supports both CSS and XPath; prefer role selectors first, then XPath for text/ancestor logic.
Practice quickly
Pick a function, craft a predicate, then run it in the XPath playground. Not sure where to start? The examples library and Selenium guide show function-heavy selectors you can copy.