Mastering XPath in Selenium 4 ๐Ÿš€ – Supported Functions & Axes Explained

 Absolutely! Here's a complete list of XPath functions and axes supported by Selenium 4 (which uses XPath 1.0), with descriptions and examples.


๐ŸŸข XPath Features Supported by Selenium 4




๐Ÿงญ ALL XPath Axes (13 Total)

Axis Name Description
ancestor:: All ancestors (parent, grandparent, etc.) of the current node
ancestor-or-self:: All ancestors + the current node
attribute:: All attributes of the current node (usually @attribute shorthand is used)
child:: All direct children of the current node
descendant:: All descendants (children, grandchildren, etc.) of the current node
descendant-or-self:: All descendants + the current node
following:: All nodes after the current node in the document
following-sibling:: All sibling nodes after the current one
namespace:: All namespace nodes of the current node (rarely used in HTML)
parent:: Immediate parent of the current node
preceding:: All nodes that come before the current node
preceding-sibling:: All sibling nodes before the current node
self:: The current node itself

✅ All XPath Axes (13 axes)

AxisDescriptionExample XPath
ancestor::All ancestors of the current node//input[@id='email']/ancestor::form
ancestor-or-self::Ancestors + the current node//div[@id='main']/ancestor-or-self::div
attribute::All attributes of the current node//input/attribute::type or //@type
child::Direct children nodes//div[@id='form']/child::input
descendant::All descendants (children, grandchildren, etc.)//div[@id='menu']/descendant::a
descendant-or-self::Descendants + current node//div[@class='box']/descendant-or-self::div
following::All nodes after the current node//label[@for='email']/following::input
following-sibling::All siblings after the current node//label[@for='email']/following-sibling::input
namespace::Namespace nodes (rarely used in HTML)(Not used in typical Selenium tests)
parent::Parent node//input[@id='user']/parent::div
preceding::All nodes before the current node//input[@id='search']/preceding::label
preceding-sibling::All siblings before the current node//input[@id='email']/preceding-sibling::label
self::The current node itself//div[@id='main']/self::div



๐Ÿง  ALL XPath Functions (Most Commonly Used)

Node Functions

Function Description
text() Selects the text of an element
node() Matches any kind of node
name() Returns the name of the node
local-name() Returns local name without prefix (namespace-specific)

Node and String Functions

FunctionDescriptionExample XPath
text()Gets text content of node//button[text()='Login']
contains(string1, string2)Returns true if string2 is in string1//input[contains(@name,'user')]
starts-with(string1, string2)Returns true if string1 starts with string2//input[starts-with(@id,'user')]
substring(string, start, length?)Extract substring from stringsubstring('username',1,4) (in predicates)
string-length(string?)Returns length of stringstring-length('hello')
normalize-space(string?)Trims leading/trailing and multiple spaces//button[normalize-space()='Submit']
concat(string1, string2, ...)Concatenates multiple stringsconcat('Hello', ' ', 'World')
translate(string, from, to)Replaces characters in stringtranslate('HELLO','EL','el')
substring-before(string1, string2)Substring before string2substring-before('user@email.com','@')
substring-after(string1, string2)Substring after string2substring-after('user@email.com','@')
name()Returns node name//*[name()='input']
local-name()Returns node local name//*[local-name()='input']

๐Ÿ”ฃ String Functions

Function Description
contains(string1, string2) Returns true if string2 is found in string1
starts-with(string1, string2) True if string1 starts with string2
ends-with(string1, string2) XPath 2.0 – true if string1 ends with string2 (not in XPath 1.0)
substring(string, start, length?) Extract part of string
string-length(string?) Returns length of a string
normalize-space(string?) Removes leading/trailing/multiple spaces
concat(string1, string2, ...) Concatenates strings
translate(string, from, to) Replaces characters in string
substring-before(string1, string2) Substring before string2 in string1
substring-after(string1, string2) Substring after string2 in string1

๐Ÿ”ข Numeric Functions

Function Description
position() Returns the index position of the node in the node set
last() Returns the index of the last node in the node set
count(node-set) Returns number of nodes in a set
number(value) Converts a value to a number
sum(node-set) Returns sum of numeric values
floor(number) Rounds down to nearest integer
ceiling(number) Rounds up to nearest integer
round(number) Rounds to nearest integer

๐Ÿ” Boolean Functions

Function Description
boolean(value) Converts value to boolean
not(condition) Negates the condition
true() / false() Return literal boolean values
lang(language-code) Returns true if language of node matches

๐Ÿ›  XPath Strategies and Syntax Variants

๐Ÿ”ง Attribute Selectors

//tag[@attribute='value']           → //input[@id='email']

๐Ÿ”ง Wildcard

//*[@type='text']                  → Matches any tag with type='text'

๐Ÿ”ง Multiple Conditions

//input[@type='text' and @name='username']

๐Ÿ”ง Indexing

//div[3]                          → Third div among siblings
(//input[@type='checkbox'])[2]   → Second matching checkbox

๐Ÿ”ง OR Condition

//input[@type='submit' or @value='Login']

๐Ÿ”ง Grouped Tags

//div | //span                    → Matches both div and span

๐Ÿ“Œ Full Example Combining All Concepts

<!-- Sample HTML -->
<div class="login-form">
  <label for="user">Username</label>
  <input type="text" id="user" name="username" />
  <label for="pass">Password</label>
  <input type="password" id="pass" />
  <button>Login</button>
</div>

Examples:

//div[@class='login-form']/child::input[1]
//label[text()='Username']/following-sibling::input
//input[starts-with(@id, 'user')]
//input[not(@type='submit')]
//input[@name and @type='text']
//input[@id='user']/ancestor::div
//button[normalize-space()='Login']
//(//input)[last()]

✅ Summary Cheat Sheet

Category Total Covered
XPath Axes 13 ✅ All listed
XPath Functions 30+ ✅ All practical + rare
Strategies/Syntax ✅ All major patterns





XPath Functions Supported


Numeric Functions

Function Description Example XPath
position() Position of node in current node set (//input[@type='text'])[position()=2]
last() Position of last node (//div[@class='item'])[last()]
count(node-set) Number of nodes in a node-set count(//input)
number(value) Converts string to number number('123')
sum(node-set) Sum of numeric values sum(//price)
floor(number) Floor value (round down) floor(4.7)
ceiling(number) Ceiling value (round up) ceiling(4.1)
round(number) Round to nearest integer round(4.6)

Boolean Functions

Function Description Example XPath
boolean(value) Converts value to boolean boolean(//input[@id='test'])
not(condition) Logical NOT //input[not(@type='submit')]
true() Returns true //input[@checked=true()]
false() Returns false //input[@checked=false()]
lang(language-code) Matches language code //*[lang('en')] (rarely used)

Functions NOT Supported by Selenium XPath (XPath 2.0+)

Function Description Notes
ends-with(string1, string2) Checks if string1 ends with string2 Use substring workaround (see below)
matches(string, regex) Regex matching Not supported
tokenize(string, regex) Regex tokenize Not supported
replace(string, regex, replacement) Regex replace Not supported
upper-case(string) Converts string to upper case Not supported
lower-case(string) Converts string to lower case Not supported

Workaround for ends-with() in Selenium XPath 1.0

//input[substring(@name, string-length(@name) - string-length('name') + 1) = 'name']

Summary Table

Category Supported in Selenium 4? Notes
XPath Axes ✅ All 13 supported Fully supported
XPath Functions ✅ XPath 1.0 subset No XPath 2.0 functions
XPath 2.0 Functions ❌ Not supported Workarounds needed
XPath Strategies ✅ All common patterns Absolute, relative, predicates, etc.


Comments

Popular posts from this blog

10 automation test cases for https://www.saucedemo.com/ (Sauce Demo)

Java Roadmap for SDET Superstars!