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.

Descendants
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


<form>

<div>
  
<title>Harry Potter</title>
  
<author>J K. Rowling</author>
  
<year>2005</year>
  
<price>29.99</price>
</div>

</form>


Here is Example:





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

//div[@id='browse-category']/descendant::*

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

Here i want to identify Payment link using descendant.

//div[@id='browse-category']/descendant::a[@data-category='Payment']

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.



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


selects all elements after closing tag of P

//p[@class='fk-font-14']/following::*




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

//p[@class='fk-font-14']/following::div[@id='fk-mainfooter-id']


Following-sibling:
Selects all siblings after the current node.
//div[@id='browse-category']/ul/li/following-sibling::*

if you want a specific element then

//form[@class='faqsearchform']/following-sibling::div





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

preceding-sibling
Selects all siblings before the current node

Here is the example:

//div[@id='browse-category']/preceding-sibling::form





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 Remove..here is the final xpath

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


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..







26 comments:

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

    ReplyDelete

  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

    ReplyDelete
  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

    ReplyDelete
  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

    ReplyDelete
  5. Hi,

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

    Thanks,
    Amit Chaudhary

    ReplyDelete
    Replies
    1. or you can use the below xpath

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



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

    ReplyDelete
  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

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


    ReplyDelete
  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

    ReplyDelete
  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

    ReplyDelete
  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

    ReplyDelete
  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

    ReplyDelete
  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

    ReplyDelete
  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

    ReplyDelete
  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

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

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

    ReplyDelete
  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

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

    ReplyDelete