XPath Functions Overview
give me comprehensive list and notes of xpath functions
Here’s a comprehensive list of XPath functions with explanations and notes, categorized by their purpose. This is
useful for both manual testers and automation testers (e.g., in Selenium).
🔹 1. Node Functions
These help in navigating and identifying elements.
Function Description Example Notes
node() Selects all types of nodes (element, attribute, //node() Rarely used directly.
text, etc.)
text() Selects text nodes //div[text()='Login'] Useful for matching visible text.
. Current node .//span Relative XPath starting from current
node.
.. Parent node .. Goes one level up.
* Wildcard for any element //div/* Selects all child elements.
🔹 2. String Functions
Used to manipulate and evaluate strings.
Function Description Example Notes
contains(str1, str2) Checks if str1 contains str2 //a[contains(text(),'Login')] Case-sensitive.
starts-with(str1, Checks if str1 starts with str2 //input[starts-with(@id,'user')]
str2)
ends-with(str1, str2) Checks if str1 ends with str2 //input[ends-with(@name,'id')] XPath 2.0+ only.
substring(string, Returns substring from start substring('OpenAI',1,4) → 'Open' Index starts at 1.
start, length) with given length
substring-after(str1, Gets part of str1 after str2 substring-after('abc@xyz.com','@')
str2) → 'xyz.com'
substring-before(str1, Gets part of str1 before str2 substring-before('abc@xyz.com','@')
str2) → 'abc'
1/4
Function Description Example Notes
normalize- Removes normalize-space(//label) Useful for
space(string) leading/trailing/multiple inconsistent
whitespaces spacing.
string-length(string) Returns number of characters string-length('Hello') → 5
🔹 3. Boolean Functions
Evaluate to true or false .
Function Description Example Notes
not(condition) Logical NOT //input[not(@type='hidden')] Negates the condition.
true() / false() Boolean constants //*[@available='true()'] Rarely used directly.
boolean(expression) Converts expression to boolean(//div[@id='login']) Often used in conditional
boolean expressions.
🔹 4. Numeric Functions
Perform calculations.
Function Description Example Notes
position() Position of current node in node-set (//li)[position()=1] Use for indexing.
last() Position of last node in node-set (//input)[last()]
count(node-set) Counts number of nodes count(//div[@class='item'])
sum(node-set) Sums numeric values sum(//price) Used in XPath 2.0+
round(number) Rounds to nearest integer round(3.6) → 4
floor(number) Rounds down floor(3.6) → 3
ceiling(number) Rounds up ceiling(3.1) → 4
🔹 5. XPath Axes (Not functions, but often used)
These allow navigation in document tree.
2/4
Axis Description Example Notes
ancestor All ancestors (parents, grandparents, etc.) //div/ancestor::section
ancestor-or-self All ancestors and the node itself //div/ancestor-or-self::*
attribute All attributes of a node //input/attribute::*
child Direct children //ul/child::li
descendant All descendants //div/descendant::input
descendant-or-self Descendants and the node itself //div/descendant-or-self::*
following All nodes after the current node //h2/following::div
following-sibling Siblings after current node //div[@class='title']/following-sibling::p
parent Parent of node //span/parent::div
preceding All nodes before current node //p/preceding::div
preceding-sibling Siblings before current node //li[2]/preceding-sibling::li
self Current node //a/self::a
🔹 6. Useful XPath Tricks & Tips
// = anywhere in the document
/ = direct child
@ = attribute
Combine conditions: //input[@type='text' and @name='user']
Use () for grouping and precedence
✅ Real-world Selenium Examples
java
driver.findElement(By.xpath("//input[@id='username']")); // Basic
driver.findElement(By.xpath("//button[contains(text(),'Login')]")); // Text-based
driver.findElement(By.xpath("//div[@class='menu']//a[starts-with(@href,'/product')]")); // Starts-with
driver.findElement(By.xpath("(//input[@type='checkbox'])[last()]")); // Last checkbox
3/4
🔹 XPath 1.0 vs 2.0
Selenium and most browsers support XPath 1.0.
XPath 2.0 adds more functions ( ends-with , matches , better regex), but not widely supported in browsers.
Would you like this as a downloadable PDF or cheat sheet as well?
4/4