A relative or an absolute xpath can be created in one of two methods. The element we want to identify can be found along the entire path in the absolute xpath, starting at the root.
The / sign denotes the beginning of an absolute xpath. The absolute xpath has the problem of losing validity if any attribute changes occur from the element’s root to any other attribute, starting with the attribute.
What is XPath?
XPath stands for XML Path Language, and it is used to navigate through elements and attributes in XML and HTML documents. It provides a way to traverse the document structure and locate specific elements or data within the document.
Absolute XPath: The Full Path
Absolute XPath uses the full path from the root element to the target element. It starts with a single forward slash (“/”) and then lists all the elements and their positions from the root to the target element. For example:
In the above example, the XPath specifies the full path from the root element (“/html”) to an input element located within a form in the first div element on the page.
Relative XPath: A Path Relative to the Context
Relative XPath, on the other hand, does not start from the root element but instead starts from a specific context within the document, often the current element or a known starting point. It uses various shortcuts and functions to navigate to the desired element. For example:
In the relative XPath above, we use double forward slashes (“//”) to indicate that we want to find an input element that is a descendant of a form element, regardless of where it is within the document. This is more flexible than absolute XPath, as it adapts to changes in the document structure.
Now that we’ve seen examples of both absolute and relative XPath, let’s delve into the key differences between them:
- Starting Point: Absolute XPath starts from the root of the document, whereas relative XPath starts from a specific context within the document.
- Stability: Absolute XPath is less stable because it relies on the exact structure of the document. If the document structure changes, even slightly, the XPath may break. Relative XPath is more robust in this regard because it’s often based on the relationships between elements rather than their absolute positions.
- Readability: Relative XPath is usually more concise and easier to read, making it a preferred choice in most cases.
- Maintenance: Relative XPath is easier to maintain because it adapts to changes in the document structure, reducing the need for frequent updates.
When to Use Each Approach
- Use Absolute XPath when the structure of the document is highly predictable, and you need to specify the exact path to a specific element.
- Use Relative XPath when the document structure is subject to change, or when you want to find elements based on their relationships with other elements (e.g., finding the second input field within any form).
In contrast to the root node, the relative xpath begins by referring to the element that we want to identify. With the // symbol, an xpath is relative. Since the relative xpath is unaffected by the DOM’s ability to add or remove elements, it is mostly utilized in automation.
The absolute xpath (html/body/tagname/…) is lengthy and challenging to manage. While a relative xpath (//*[@attribute=’value’]) is brief. Let’s find the Home menu on the page below.
Starting at the base, let’s examine the Home element’s HTML code.
The element’s xpath is as follows: /html/body/div/div/div/a. It can be checked using the $x(“/html/body/div/div/div/a”) expression in the console of the browser (opened by pressing F12).
This element’s relative xpath is //a[@title=”softwaretestingstudymaterials- Home”]. The expression $x(“//a[@title=’TutorialsPoint – Home’]”) in the browser Console can be used to confirm it. The matching element from the relative xpath expression is displayed in the image.
XPath is a valuable tool for web scraping and automation, and understanding the difference between absolute and relative XPath is essential. While absolute XPath provides the full path from the root element, relative XPath offers flexibility and adaptability. In most cases, relative XPath is the preferred choice due to its readability and stability, especially when dealing with dynamic web pages.
By mastering both absolute and relative XPath, you’ll be better equipped to navigate and extract data from web documents effectively, whether you’re scraping data for analysis or automating web interactions.