What are the different types of XPath in Selenium?

What are the different types of XPath in Selenium?

Hi Emma,

The XPath is a query language for selecting nodes from an XML document. An XPath expression is a string that specifies the location of the nodes relative to their siblings, or more specifically, the location of elements relative to their parent element.

Here are the two main types of XPath in Selenium:

Absolute XPath

As the name indicates, absolute XPath contains the complete path from the root element to the desired element. It is ideal for storing XPath in a file which can then be used anytime later with any HTML page.

Syntax: /html/body/div[x]/div[y]/

Relative XPath

In relative XPath element is positioned relative to its normal position. It starts by referencing the element we want to locate relative to a particular location.

Syntax: //tagname[@attribute=‘value’]

To get into details, please refer to the following XPath cheat sheet:

1 Like