Advise on hierarchy for element locators

Answered

Hi, whilst I am aware of how to search for the different elements on a page, e.g.by xpath, id, css etc, I was after some advise as to which hierarchy to use and why? E.g. should we use xpath locators before reverting to searching for the id?

Any help would be appreciated.

Train Asked on December 8, 2014 in Selenium WebDriver.
Add Comment
  • 3 Answer(s)
    Best answer

    Hi Andy,

    I am not sure whether people will agree on my view or not. For me always prefer locators first => ID & Name
    Second preference would be CSS selector, as they are fast
    Third preference would be Xpath

    But you will see that most of the time after locators, users prefer xpath. And there is no harm in that, as you might not able to notice any performance difference between xpath and CSS selector but few practical tests on large number of scripts has proven this. Still users are much more comfortable with xpath and it is widely used now. May be because of Firebug and Firepath tool, it is very easy to get absolute and relative xpaths using these tools.
    Regards,
    Lakshay Sharma

    Professor Answered on December 8, 2014.

    Hi Laksh, many thanks. From what I’ve read it appears most people go along with your viewpoint. The only concern I have with using Xpath (more with absolute xpath) is that all it takes is for one single section of the HTML to change, for the web driver test to fail. However, in some scenarios where we have to use xpath I think it’s unavoidable

    on December 8, 2014.
    Add Comment

    Hi Andy,

    Thanks for supporting my answer Andy. Yes absolute xpaths are never advisable and we should always use relative xpath. I request you to download the tool Firepath and it will automatically generate relative xpath for you, you dont need to learn. Plus I am sharing the URL for Firepath usage and Effective xpaths techniques:

    Firebug and Firepath
    Choose Effective xpath

    Please close the answers.

    Regards,
    Lakshay Sharma

    Professor Answered on December 9, 2014.
    Add Comment

    Hi Lakshay, that’s great thanks. I’ll close off the answer now 🙂 Hope you’re well anyway. Andy

    Train Answered on December 9, 2014.

    I am good Andy,
    Thats how we close the answer 🙂
    http://forumsqa.com/selenium-webdriver/how-to-choose-best-answer-insert-code-vote-up-an-answer/

    Click on you name and you will find there are 3 more unclosed questions on your name, please close them as well.

    on December 9, 2014.

    OOps, cheers Laksh. Hopefully done that correctly now 🙂

    on December 9, 2014.
    Add Comment
  • Your Answer

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