Pages

Search This Blog

Sunday, July 6, 2014

How to use preceding-sibling and following-sibling in xpath to find sibling nodes

How to use preceding-sibling and following-sibling in xpath to find sibling nodes


How to get all the preceding siblings of Apple

Xpath: "//ul/li[contains(text(),'Apple Mobiles')]/preceding-sibling::li"

This will give "Samsung Mobiles"

How to get all the following  siblings of Apple
Xpath: "//ul/li[contains(text(),'Apple Mobiles')]/following-sibling::li"

This will give all the preceding siblings ( Nokia Mobiles, HTC Mobiles, Sony Mobiles, Micromax mobiles)

There is trick to use preceding-sibling and following-sibling. Place matters when you use this at beginning it will give you reverse result
When you use preceding-sibling at beginning then it will give result  ( Nokia Mobiles, HTC Mobiles, Sony Mobiles, Micromax mobiles) instead of Samsung mobiles.

Xpath : "//li[preceding-sibling::li='Apple Mobiles']"

This will give Samsung mobiles.
when you use following-sibling at the beginning then it will give reverse result. Instead of giving all below nodes of Apple mobile this will give Samsung Mobiles.
Xpath: "//li[following-sibling::li='Apple Mobiles']"
Now the question is how to get all the nodes between Apple Mobiles and Sony Mobiles.

Xpath : "//ul/li[preceding-sibling::li='Apple Mobiles' and following-sibling::li='Sony Mobiles']"
This will return Nokia Mobiles and HTC Mobiles.

or
Xpath : "//ul/li[preceding-sibling::li[.='Apple Mobiles'] and following-sibling::li[.='Sony Mobiles']]"
Or You can use this in contains as well
Xpath: "//ul/li[preceding-sibling::li[contains(text(),'Apple Mobiles')] and following-sibling::li[contains(text(),'Sony Mobiles')]]"

20 comments:

  1. i have a question regarding xpath following -sibiling
    there is application(http://www.zlti.com/)in that menus are there which contain list links.i want to mouseover one menu and get all the list of liks and test those links by clicking .For that i need script.i have tried upto mouseover menu and perform clicking operation.But not able to finding list of links.pls give solution for that scenario.

    ReplyDelete
  2. This comment has been removed by a blog administrator.

    ReplyDelete
  3. https://chrome.google.com/webstore/detail/relative-xpath-helper/eanaofphbanknlngejejepmfomkjaiic

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

    ReplyDelete
  5. the best and very useful blogs for learner each and every concept explain in easy way thank you for sharing such a nice content.keep on update latest information all the best
    Selenium Online Training usa|uk|canada|australia

    ReplyDelete
  6. A nice article here, i think that people who have grown up with the idea of using computers are showing more responsibility towards writing posts that are thoughtful, do not have grammar mistakes and pertinent to the post..

    Selenium Training in Chennai

    ReplyDelete
  7. This is very informative blog and article thank you for sharing with us keep posting more information about salesforce trainingselenium training, selenium online training, selenium course,learn selenium course

    ReplyDelete
  8. It was very nice blog to learn about Selenium.Thanks for sharing new things.selenium training in chennai

    ReplyDelete
  9. A fantastic blog to know what Selenium is all about. It can be used in combination with many basic core programming languages for testing applications. Selenium online training in hyderabad, best selenium online course

    ReplyDelete
  10. Advanced Selenium Framework Training in Chennai by Vishwa

    Hi, Reach Mr.Vishwa for Best Selenium and Coded UI Training in Chennai with real time project assistance. This people will teach everything from the basics up to advanced level scenarios from frameworks.

    I learned from Mr.Vishwa and Raj they are very good automation people for Selenium Frameworks. After joined with them I learned many things and now I am writing automation scripts in own project. Vishwa 9003085882
    selenium training in chennai

    ReplyDelete
  11. thanks for sharing topic on How to use preceding-sibling really very useful post keep sharing

    Selenium Training in Chennai

    ReplyDelete
  12. It was very nice blog to learn about Selenium.Thanks for sharing new things.selenium training in chennai

    ReplyDelete
  13. Selenium Testing Real Time Hands on Training in Chennai…

    Don’t always Depend on Training Institute alone.Please aware of Best Trainers too..

    http://thecreatingexperts.com/selenium-training-in-chennai/

    If You need a Best Trainer in Selenium Testing??? Then be ready for a DEMO From the Trainer MR.Karthick
    CONTACT:8122241286

    Both Classroom/Online Training is Available!!!!!!

    ReplyDelete