16

What is the difference between absolute and relative xpaths? Which is preferred in Selenium automation testing? I am preparing test scripts using Selenium and Robot framework.

Mathias Müller
  • 22,203
  • 13
  • 58
  • 75
Shashank
  • 159
  • 1
  • 1
  • 4

5 Answers5

19

Absolute Xpath: It uses Complete path from the Root Element to the desire element.

Relative Xpath: You can simply start by referencing the element you want and go from there.

Relative Xpaths are always preferred as they are not the complete paths from the root element. (//html//body). Because in future, if any webelement is added/removed, then the absolute Xpath changes. So Always use Relative Xpaths in your Automation.

Below are Some Links which you can Refer for more Information on them.

Elijah
  • 1,814
  • 21
  • 27
Rupesh Shinde
  • 1,933
  • 12
  • 22
6

An absolute xpath in HTML DOM starts with /html e.g.

/html/body/div[5]/div[2]/div/div[2]/div[2]/h2[1]

and a relative xpath finds the closed id to the dom element and generates xpath starting from that element e.g.

.//*[@id='answers']/h2[1]/a[1]

You can use firepath (firebug) for generating both types of xpaths

It won't make any difference which xpath you use in selenium, the former may be faster than the later one (but it won't be observable)

Absolute xpaths are prone to more regression as slight change in DOM makes them invalid or refer to a wrong element

adnan kamili
  • 8,967
  • 7
  • 65
  • 125
  • 1
    This answer is simply wrong. An absolute XPath expression is **not** one that starts with "html" - it's one that starts with `/`. What you are describing seems to be "positional" path expressions vs. referring to elements by an ID. – Mathias Müller Feb 03 '16 at 11:42
  • @MathiasMüller The answer is specific to html dom not generic, in general its not true. Since selenium was mentioned the answer is valid for user. – adnan kamili Feb 03 '16 at 12:44
  • 1
    @adnankamili I'm not sure that's true. [This page](http://www.seleniumhq.org/docs/02_selenium_ide.jsp#locating-by-xpath) for instance suggests that a path starting with `/html` would be an absolute path. – Mathias Müller Feb 03 '16 at 13:24
  • @MathiasMüller is correct; **this answer is wrong**. `html/body` is a [relative location path](https://www.w3.org/TR/xpath/#NT-RelativeLocationPath); `/html/body` would be an [absolute location path](https://www.w3.org/TR/xpath/#NT-AbsoluteLocationPath). Please either correct your answer or delete it. Thanks. – kjhughes Oct 06 '16 at 18:55
  • @adnankamili - What about this xpath - //div[@id='123']/div ? This would be similar to absolute xpath, right ? – MasterJoe Mar 27 '18 at 00:34
  • @MasterJoe No it's not, absolute path has to be started with `/`. – S.B Jul 22 '21 at 22:14
  • @MasterJoe More critically, the `//` means search recursively. In several critical ways this differs from the assumptions inherent when using absolute (x)paths. It's a necessary but not sufficient description of an absolute XPath to say that it begins with `/`. – mcint Jul 18 '23 at 01:27
1

Consider Below Html

<html>
  <body>
    <input type ="text" id="username">
  </body>

</html>

so Absoulte path= html/body/input and Relative path = //*[@id="username"]

Disadvantage with Absolute xpath is maintenance is high if there is nay change made in html it may disturb the entire path and also sometime we need to write long absolute xpaths so relative xpaths are preferred

MD5
  • 1,356
  • 15
  • 14
1

Absolute XPath: It is the direct way to find the element, but the disadvantage of the absolute XPath is that if there are any changes made in the path of the element then that XPath gets failed.

The key characteristic of XPath is that it begins with the single forward slash(/) ,which means you can select the element from the root node.

Below is the example of an absolute xpath.

/html/body/div[1]/section/div/div[2]/div/form/div[2]/input[3]

Relative Xpath: Relative Xpath starts from the middle of HTML DOM structure. It starts with double forward slash (//). It can search elements anywhere on the webpage, means no need to write a long xpath and you can start from the middle of HTML DOM structure. Relative Xpath is always preferred as it is not a complete path from the root element.

Below is the example of a relative XPath.

//input[@name=’email’]
Kamran
  • 669
  • 6
  • 9
1

We can say mainly there are 3 differences:

  1. Absolute will start with / where as Relative will start with //
  2. Attributes is not used in absolute where as attribute is used in relative
  3. Absolute is a complete path where as relative is partial path

Mainly Relative is preferred, Absolute is not preferred because since its a complete path and elements can change because of dynamic nature so there may be breakage of path so relative is preferred.

bad_coder
  • 11,289
  • 20
  • 44
  • 72
Bujju
  • 11
  • 1