March 3, 2016

Advanced Xpath with Examples

Here i am going to discuss about advanced usage of xpaths..
I will provide examples how to use xpath Axes.

A node's children, children's children, etc.
In the following example; descendants of the form element are the div, title, author, year, and price elements


<title>Harry Potter</title>
<author>J K. Rowling</author>


Here is Example:

If you want to find descendants of Div tag you need to use below syntax


If you want a specific element from descendants then use below syntax

Here i want to identify Payment link using descendant.


In the above example descendant will search for child elements and child's child elements as well.

UL is the child element for DIV and LI is the child element for UL and A is child element for LI. As we used descendant it will check for the matching element.

Selects everything in the document after the closing tag of the current node.

selects all elements after closing tag of P


if you want a specific element using following below is the example


Selects all siblings after the current node.

if you want a specific element then


Preceding :
Selects all nodes that appear before the current node in the document, except ancestors, attribute nodes and namespace nodes

Selects all siblings before the current node

Here is the example:


Now Lets see a real time example how to use the above mentioned xpath Axes:

Below is scenario:

  1. Navigate to flipkart
  2. add two products to cart
  3. I want to remove one product based on its name.

Simple xpath without using product name --- //a[@class='cart-remove-item fk-inline-block fk-uppercase'] but it always identifies first Remove button.

First Step: I will identify xpath for product Name

//span[text()='HP 15-af114AU Notebook(AMD Quad Core A8/ 4GB/...']

From there onward using xpath Axes i need to identify Remove link.

By loooking at DOM we can understand that there are two TR tags 
in one TR --we have product details and in another TR we have remove link.

so i have to traverse to second TR.

Second Step: Inorder to do that i need to traverse to parent element of span

//span[text()='HP 15-af114AU Notebook(AMD Quad Core A8/ 4GB/...']/ancestor::tr[1]

Third Step: from there traverse to sibling of TR
//span[text()='HP 15-af114AU Notebook(AMD Quad Core A8/ 4GB/...']/ancestor::tr[1]/following-sibling::tr

Fourth Step: From there traverse to tag A which has link text of is the final xpath

//span[text()='HP 15-af114AU Notebook(AMD Quad Core A8/ 4GB/...']/ancestor::tr[1]/following-sibling::tr

Hope this is useful in writing advanced xpaths....if you have any questions please do comment and I will be happy to help in you in writing xpaths..


  1. Nice explanations. Could you please explain it one more example where such xpaths are used .
    Thank you in advance.


  2. Superb i got some valuable tips from here. Really its a amazing article i had ever read. I hope it will help a lot for all. Thank you so much for this amazing posts

    and please keep update like this excellent article.

    SAP MM Training in Chennai

  3. Another valuable information,thanks for sharing that valuable information.Given example such as really wonderful,it's more helpful in my studies.
    php training in chennai

  4. Thanks for sharing great information in your blog. Got to learn new things from your Blog . It was very nice blog to learn about Selenium

  5. Hi,

    Can you please help with writting xpath for "LOGIN" text on window appearing after clicking "Login" link of flipkart homepage ?

    Amit Chaudhary

    1. or you can use the below xpath

      //div[@id='fk-mainbody-id']//input[@class="fk-input login-form-input user-pwd"]

  6. //div[@id='fk-mainbody-id']//input[@type="password" and @placeholder="Enter password"]

  7. I genuinely appreciate the hard work you people put for providing such with such useful and excellent information. SAP Simple Finance Training in Hyderabad

  8. advanced xpath with example thanku for sharing this ncie posts..
    sap netweaver training in hyderabad

  9. SAP HANA training in hyderabad,This is the best path You can Learn COmplete Course with full fledge knowledge of SAP.
    SAP HANA online training

  10. very nice blogs, i have to learning for lot of information for this sites. Sharing for wonderful information. Our on-line coaching supplier gained the high commonplace name through worldwide for its teaching.
    Oracle fusion financials training

  11. Hi,
    Very useful and informative article you have been provided to us.Thanks for sharing such a article and sharing your knowledge with your users.
    Thank you,
    Oracle EBS training

  12. hello,
    Very useful and informative article you have been provided to us.thanks for sharing such a article and sharing your expertise together with your users.
    thank you,
    Oracle Fusion procurement Online Training
    Oracle Fusion cloud procurement Online Training

  13. really you have shared an informative blog. it will be really helpful to the peoples who are all working under the technology. thank you for sharing this blog.
    selenium training in chennai

  14. CALFRE handles oracle fusion financials online training and its modules maintaining classroom based training with the self-paced videos. An expert having ten plus years of self-experience handles the training period through online and explains each and every point perfectly. We recently launched our institute in the USA and getting
    the best reputation over there.

    Oracle fusion Financials Training in hyderabad

    Oracle Fusion Financials online Training in hyderabad

  15. really you have shared an informative blog. it will be really helpful to many peoples who are all working under the technology. thank you for sharing this blog.
    java training in chennai

  16. Nice information about sap. i am looking SAP HANA Online Training with real time project.

  17. Useful information. i am looking for SAP HANA Online Training from professional expert.

  18. You made some good points there. I did a search on the topic and found most people will agree with your blog... Selenium Training in Chennai | Selenium Training institutes in Chennai

  19. This site has lots of advantage awesome i really enjoyed reading thanks for sharing for grate info ..
    sap mm training

  20. myTectra Placement Portal is a Web based portal brings Potentials Employers and myTectra Candidates on a common platform for placement assistance

  21. I am happy to find this post Very useful for me, as it contains lot of information

    Guest posting sites

  22. Innovative thinking of you in this blog makes me very useful to learn.
    i need more info to learn so kindly update it.
    Angularjs Training Bangalore
    Angular JS Training courses near me
    AngularJS Training in Amjikarai

  23. Hmm, it seems like your site ate my first comment (it was extremely long) so I guess I’ll just sum it up what I had written and say, I’m thoroughly enjoying your blog. I as well as an aspiring blog writer, but I’m still new to the whole thing. Do you have any recommendations for newbie blog writers? I’d appreciate it.
    Advanced AWS Training in Bangalore | Best Amazon Web Services Training Institute in Bangalore
    Advanced AWS Training Institute in Pune | Best Amazon Web Services Training Institute in Pune
    Advanced AWS Online Training Institute in india | Best Online AWS Certification Course in india
    AWS training in bangalore | Best aws training in bangalore

  24. Great Article… I love to read your articles because your writing style is too good, its is very very helpful for all of us and I never get bored while reading your article because, they are becomes a more and more interesting from the starting lines until the end. 
    microsoft azure training in bangalore
    rpa training in bangalore
    best rpa training in bangalore
    rpa online training

  25. Nice post. By reading your blog, i get inspired and this provides some useful information. Thank you for posting this exclusive post for our vision. 
    Best Devops online Training
    Online DevOps Certification Course - Gangboard

  26. Fantastic work! This is the type of information that should follow collective approximately the web. Embarrassment captivating position Google for not positioning this transmit higher! Enlarge taking place greater than and visit my web situate
    Python Online certification training
    python Training institute in Chennai
    Python training institute in Bangalore