What does the concept of XPath mean?

XPath is a language used to find information in XML structures (including HTML).

XPath is used to examine elements and their properties in an XML document.

XPath is used to navigate XML with the help of path expression.

It is used to locate any element on a web page using HTML and DOM structure.

XPath Types

There are two different types of XPath.

Absolute XPath and Relative XPath

Absolute XPath

It’s the direct way to find the item, but the downside of Absolute XPath is that XPath fails if any change is made to the item’s path.

This is not recommended. Inspect mode is opened on the web with the F12 key. Click on Elements. After the item is found, Copy – Copy XPath is selected by right-clicking again.

You can see the Absolute XPath example below.

Absolute xpath:

/html/body/div[3]/div[1]/header/div[2]/div/div/div/div/div[2]/div[1]/nav/ul/li[1]/a /div/span[1]

Relative XPath

For Relative Xpath, the path starts from the middle of the HTML DOM structure. It begins with a double slash (//), meaning it can search for the item anywhere on the webpage.

You can start from the middle of the HTML DOM structure, and you don’t need to type long Xpaths.

Below is an example of a relative XPath expression of the same item shown on the screen.

This is the standard format used to find the item via Relative XPath. The benefit of Relative XPath is that it is more usable and requires less maintenance/cost.

Relative xpath: //*[@class=“has-submenu”]//span[text()=“Product”]