How to write Relative Xpath

To choose the relative xpath, it is advisable to look for the recent Id attribute. Look below at the HTML code of the above screen shot.Locators-4

You can see the recent or last Id produced is ‘footer_nav‘. This id would be appropriate in this case, so a quality xpath will look like this:  //*[@id=’social-media’]/ul/li[3]/a

Did you notice the difference between the Absolute and Relative xpaths?

Absolute xpath: /html/body/div[2]/div/div/footer/section[3]/div/ul/li[3]/a

Relative xpath: //*[@id=’social-media’]/ul/li[3]/a

Here , I am not getting how you written Relative Xpath ?

Murali Train Asked on December 17, 2014 in Selenium WebDriver.
Add Comment
4 Answer(s)

First you have to inspect the RSS feed with firebug from the demo site “http://store.demoqa.com/”
and then write the relative xpath manually.

RE: How to write Relative Xpath
Please refer to attached screenshot.

The RSS feed has a div id as “social media

ul is the first child of the div id hence //*[@id=’social-media’]/ul

as we are referring to the third “li” tag hence “li[3]”

ans finally inside li[3] we are referring to “a” tag hence the final relative path goes like “//*[@id=’social-media’]/ul/li[3]/a”

rajesh Supporter Answered on December 17, 2014.

@Rajesh, nice explanation. Voted up.

on December 17, 2014.

Good One Rajesh. Voted Up :)

on December 17, 2014.

I was wondering that, I feel so dummy using Firepath all the time, hehe. Thanks.

on December 17, 2014.

ThankQ Rajesh, I got it.

Finally , I have one Question, I got it from your explanation, our parent element is “div”

1. Why we are coming from “div id”, What about BEFORE and AFTER “div” ?

on December 18, 2014.
Add Comment

The single quotes mentioned in the above relative path is the issue.

manually enter the single quotes again and it works.

rajesh Supporter Answered on December 17, 2014.
Add Comment

Hi Rajesh

ThankQ, But I am not getting how

Relative xpath: //*[@id=’social-media’]/ul/li[3]/a

it is written manually ?

what is “social media” ? where we are getting “social media” from the above code ?

what is ul ? where we are getting?

what is li[3] ? where we are getting ?

what is a ? where we are getting ?

Murali Train Answered on December 17, 2014.
Add Comment

Thank you all for voting.

@Murali: We have to be very specific while choosing the right Xpath as if there is any kind of duplicate in tags or its attribute the Xpath may fail to capture the element or might pickup any different element.

You can choose any Xpath that can be uniquely identified.Like for the above scenario you can also have the Xpaths like

1. //*[@title=’Get Fed on our Feeds’]

2. //*[@class=’rss’]/a

However in this case you have to be careful that the title = “Get Fed on our Feeds” OR
class “rss” must be present only once (where u r inspecting) on the page.

rajesh Supporter Answered on December 18, 2014.
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.