Absolute & Relative Xpaths

As we have seen, how can we generate the X-path using firebug and fire path? In these days, it’s quite easy to generate x-paths. Later on, we will see how we can customize x-paths.


Let’s see the x-path of FirstName field on Facebook.com registration page.

All you need to do is-

  • Open the Facebook.com in Firefox.

  • Open the firebug.Click file path in firebug interface.

  • Click ‘Inspect Element’ in firebugClick on the WebElement(First name on facebook registration page)


Absolute XPaths

So this is the way, how we can find the XPath.


Video Tutorial:

If you liked this video, then please subscribe to our YouTube Channel for more video tutorials.



1. Absolute XPath – It is also called complete or full XPath. Absolute XPath starts from <html> tag or it starts from single slash(/). Now the question is how can we generate the absolute XPath.


It’s easy to generate the complete XPath. We can generate it using firebug and fire path. To generate the absolute XPath, you need to follow the following steps –

  1. Open your browser.

  2. Open firebug using firebug icon.

  3. Click on Firepath in firebug interface.

  4. Select the option ‘Absolute XPath’.

Now firebug will show you absolute XPath of the web element. Let’s see the XPath ofFirstName web element present on the facebook registration page.

Generate XPaths

Generate XPaths


So the relative xpath of the WebElement (First Name)  is — //*[@id=’u_0_1′]

Have you noticed, the xpath displayed in firebug is ‘.//*[@id=’u_0_1′]‘ and we are saying our xpath is ‘//*[@id=’u_0_1′]‘. i.e We are not considering ‘.‘ in the relative xpath.


Yes, Note this point. You have to remove this ‘.'(dot) while copy relative XPath from fire path and use this XPath in your script. Otherwise, your script will not work.

So our WebElement xpaths are –

Absolute – html/body/div[1]/div[2]/div[1]/div/div[1]/div/div/div[2]/div[2]/div/div/div/form[1]/div[1]/div[1]/div[1]/div/div/input

Relative – //*[@id=’u_0_1′]


Difference between single ‘/’ or double ‘//’

A single slash at the start of XPath instructs XPath engine to look for element starting from a root node.

A double slash at the start of XPath instructs XPath engine to search look for matching element anywhere in the XML document.


Let’s see the use of our generated XPath’s, Now we will see how we can use this XPath in our Selenium scripts. Let’s design a Test Case to enter Name in Firstname field of the Facebook registration page.

Test case — Enter Name in First Name field on a Facebook registration page.

Let’s understand the above-written automation script.


Here, We are storing the Web Element’s location in the fName variable, usingfindElement() method.

You noticed we have passed FirstName Web elements XPath to the variable. It means this variable will locate the FirstName field on the Facebook Registration page.


Using sendKeys() method we are typing the text in that particular web element(Firstname Textbox).

So this is all how we can find webWlement using XPath and perform the action on that. Copy the above code and try to run at your local to see the Result also try the same or different example with Absolute XPath.


It’s recommended to use relative XPath instead of absolute XPath. As it is short and perform fast. It also minimizes the risk of throw the error ElementNotFound error.

If Selenium will not find any web Element at your given location it will throw error ‘Unable to locate element‘ and mark Test Fail.

0 Comment

Leave a Reply