1. XPath Basics
2. XPath Functions
3. XPath Axes
XPath Syntax:
· Selecting Nodes
· Selecting Unknown Nodes
· Selecting Several Paths
· Absolute and Relative
· Parents and Children
· Index
· Nested locators
Absolute and relative XPath
Absolute XPath
· The path from root element to targeted element without missing any elements in between.
· Absolute XPath starts form by using ‘/’ (single forward slash) and ‘[index]’ (square bracket with index).
· /html/body/div[2]/div/div[1]/form/input[2]
Relative XPath
· This XPath is specific to the target element. It uses expression to locate web element(s) in HTML documents.
· It uses “//” (double forward slash) and “/” along with other symbols, functions, and axes names.
· //*[@id="file-submit"]
Selecting Nodes
Selecting Unknown Nodes
Selecting Several Paths
We can select several paths by using the | operator in an XPath expression.
Parents and Children
//Tag[@condition]/.. # Parent
//Tag[@condition]/Tag[condition] # Child
//Tag[@condition]/Tag[condition] # Search within child)
Index
//Tag[@condition][index] // nth child of its parent
(//Tag[@condition])[index] // nth locator of this locator
Nested Locators
//Tag[tag[@condition]] // nested locator
//Tag[.//tag[@condition]] // nested locator
XPath Functions
contains()
Selects elements whose attributes or text contain a specified value.
//span[contains(text(),'Add')]
//p[contains(text(),'Topics Covered')]
starts-with()
Selects elements whose attributes or text start with a specific value.
//span[starts-with(text(),'Add')]
//a[starts-with(text(),'Basic')]
//p[starts-with(@class,"bg-secondary text-white")]
position()
Selects elements based on their position in the document.
//tr[position()= 3]
//p[position()=4]
last()
Selects the last element in a sequence.
//tbody/tr[last()]
//p[last()]
count()
Counts the number of nodes that match a certain condition.
//tbody/tr/td[count(//thead/tr/th[contains(text(), 'Date')+1])]
normalize-space()
If an element has spaces in its text or in the value of any attribute, then to create an XPath for such an element we have to use the normalize-space function. It removes all the trailing and leading spaces from the string. It also removes every new tab or line existing within the string.
//th[normalize-space(text()) ='Date']
translate()
Replaces characters in a string with other characters.
//th[translate(text(), 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz') =' date ']
round()
Rounds the number within the text or attribute to the nearest whole number.
//td[round(text())='9']
floor()
Rounds down the number within the text or attribute to the nearest whole number.
//td[floor(text())='8']
not()
Selects elements that do not match a specified condition.
//input[@type="radio" and not(@value='2’)]
string-length()
Returns the length of the string (i.e., the number of characters in the text or attribute value).
//span[string-length(text()) = 10]
//span[string-length(text()) > 30]
//span[string-length(text()) < 4]
XPath Axes
parent
child
ancestor
ancestor-or-self
descendent
descendent-or-self
following
following-sibling
preceding
preceding-sibling
parent
Selects the direct parent of the current node (one level up in the hierarchy).
//span[text()="Starbucks coffee"]/parent::td/parent::tr/td[5]/span
child
Selects direct child nodes of the current node.
//form/child::div/div/input
ancestor
Selects all ancestor elements of the current node (parent, grandparent, etc.), moving up the DOM tree until the root, but excluding the current node itself.
//span[text()="MailChimp Services"]/ancestor::table //span[text()="MailChimp Services"]/ancestor::tr
ancestor-or-self
Selects the current node and all of its ancestors (the parent, grandparent, etc.), up to the root of the document.
//span[text()="MailChimp Services"]/ancestor-or-self::span //span[text()="MailChimp Services"]/ancestor-or-self::td
descendent
This function will return the descendant element of the particular element.
//div[@class="table-responsive"]/descendant::tr
//div[@class="table-responsive"]/descendant::td
descendent-or-self
Selects the current node and all its descendants (child, grandchild, etc.) down the DOM tree. It includes the node itself in the selection, hence "descendant-or-self."
//div[@class="table-responsive"]/descendant-or-self::div //div[@class="table-responsive"]/descendant-or-self::td
following
This function will return the immediate element of the particular component
Selects all elements that appear after the current node in the document, regardless of their level in the DOM hierarchy.
//tbody/tr[2]/following::td
//tbody/tr[2]/following::tr
following-sibling
Selects sibling elements that appear after the current node at the same level (i.e., share the same parent).
//tbody/tr[2]/following-sibling::tr
//tbody/tr[2]/following-sibling::td
Preceding
This function will return the preceding element of the particular element
//tbody/tr[2]/preceding::tr
//tbody/tr[2]/preceding::td
preceding-sibling
Selects sibling elements that appear before the current node at the same level (i.e., share the same parent).
//tbody/tr[2]/preceding-sibling::tr
//tbody/tr[2]/preceding-sibling::td
Conclusion :
This blog provides a comprehensive overview of XPath, a powerful tool used to navigate XML and HTML documents. It covers XPath basics, such as selecting nodes, and paths, and understanding the difference between absolute and relative XPath. The blog explains the use of XPath functions like contains(), starts-with(), position(), last(), and more, which help in locating elements based on specific conditions. Additionally, it delves into XPath axes, including parent, child, ancestor, descendant, following, and their variants, which are used to traverse up, down, and across the DOM hierarchy to find specific elements efficiently. These concepts are crucial for web automation and web scraping tasks, making XPath a versatile tool for developers and testers alike.
Comments