XPath Tutorial for Automation

Introduction

XPath allows the selection of elements, attributes, etc. of XML (The Extensible Mark-up Language) documents, web pages for processing in Web automation scripts. As an analogy, XPath is something similar to SQL used on database schema to select or retrieve data from the database.

XML

XML is a standard syntax for the markup of data and documents.XML documents contain one or more elements. The text contained between the start tag and the end tag is the element’s content.

An XML Element may have attributes, which will provide information about the element type or its content.

Absolute XPath vs Relative XPath

The absolute XPath starts with the root node or a forward slash (/).
The advantage of using absolute is, it identifies the element very fast.
The disadvantage of using absolute path is if some other tag added in between, then this path would be broken.

 

For Example:  /html/head/body

A relative XPath is one where the path starts from the node of your choice – it doesn’t need to start from the root node. It starts with Double forward slash(//)

For Example: //table/tbody/tr

  • Using relative XPath is simple and you don’t have to mention the long XPath.
  • Relative XPath would take more time in identifying the element as we specify the partial path, not the exact path.
  • If there are multiple elements for the same path, it will select the first element that is identified

XPath Axes

XPath axis tells the XPath processor which direction to head in as it navigates around the hierarchical tree of nodes.

For example:

  • self: Selects the current node
  • ancestor: Selects all ancestors (parent, grandparent, etc.) of the current node
  • attribute: Selects all attributes of the current node
  • child: Selects all children of the current node