Understanding XPath



What is an XPath?

XPath stands for XML Path Language. It is a query language used for locating nodes in an XML document. A webpage is built on HTML, but since HTML is also one form of XML, XPaths are widely used in HTML documents by Test Automation Engineers to locate various elements on a webpage.

XML Image



Types of XPaths

There are 2 types of XPaths,

  1. Absolute XPath: These are the XPath expressions containing full path starting from the root node to the concerned node.
  2. Relative XPath: These are the XPath expressions which provides the path relative to a specific reference node.


Key Concepts of XPath

Let us explore some of the key concepts used in XPaths

1. Nodes

Node forms the building block of an XML document. A node can be an element, attribute, text, comment or a processing instruction within an XML or HTML document. It starts with angular brackets such as <h1>, <input>, <img>, <a href=””>, etc.

Some of the common types of node are:

  1. Element Nodes - These represents any HTML or XML element.
  2. Attribute Nodes – These represents the attributes of an element.
  3. Text Nodes – These represents the textual content of an element

2. Path Expressions:

XPath expressions are strings which are used to locate the nodes in an XML or HTML document. It consists of a sequence of steps containing a combination of node, their filtering criteria and are separated using slashes (/).


3. Axes

Axes are the set of keywords used in an XPath expression to define the relationship between two nodes. These are used to locate an element using the relative location with respect to a reference element.

Some of the commonly used axes are

  • child
  • parent
  • ancestor
  • descendant
  • following-sibling
  • preceding-sibling
  • attribute

We shall explore these axes in depth in further tutorials.


4. Predicates

Predicates are nothing but the filtering criterias used to filter nodes based on certain conditions. They are represented within the square brackets [], against any node.

Follow Me on Social Media

Advertisement
FREE Sales CRM Software

Fully customizable CRM Software for Freelancers and Small Businesses

Signup for Free

Sign up for DigitalOcean Cloud

Get FREE $200 credits, deploy your hobby projects for Free

DigitalOcean Referral Badge
Sign up for Hostinger Cloud or VPS plans and get upto 20% Discount