2

I am a noob in Selenium testing. I am developing test scripts that is to be tested across different browsers (Chrome, Firefox, IE and Opera). I know the following locators can be used to identify web elements

  1. id
  2. Name
  3. Tag Name
  4. Class Name
  5. Css Selector
  6. Link Text
  7. Partial Link Text
  8. Xpath

but my questions are

  • How can I choose best selector considering the test performance? (both in terms of speed and reliability)
  • Which element selectors are faster and which are slower?
  • How can I choose selector to run the test across browsers?

closed as primarily opinion-based by IAmMilinPatel, Yu Zhang, NarendraC, Kate Paulk, Bharat Mane Nov 10 '16 at 12:14

Many good questions generate some degree of opinion based on expert experience, but answers to this question will tend to be almost entirely based on opinions, rather than facts, references, or specific expertise. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 5
    Why this question has been voted by 4 persons as opinion-based if all criteria the OP asked are objective? 1) speed and reliabiality, 2) faster and slower. The last part of the question even asks for criteria to choose a selector depending on yet another objective factor: a browser or a set of browsers. – dzieciou Nov 3 '16 at 15:04
3

TL;DR

George Ritzmann has the correct answer.

  1. ID
  2. ClassName
  3. LinkText
  4. PartialLinkText
  5. CssSelector
  6. XPath

Non-TL;DR

To understand the execution time of a find we must first understand what a find does and why the different By's would have different execution speed and what could cause it.

So we start with the Find methods inside of the RemoteWebDriver object. This calls to execute DriverCommand.FIND_ELEMENT. Execute calls the HttpCommandExecutor which sends the find request to the WebDriver.

I am going to skip over a few steps at this point because quite frankly, it's extremely convoluted. At this point though the request is forwarded on into the browser. Based on older implementations of the WebDriver, it was solely JS driven. The issue with it being JS driven is that it will vary greatly from one browser to the next.

This can be evident by using IE, especially older versions. IE's JS engine was notoriously slow for XPath and slightly quicker for CssSelectors.

Since this implementation takes place differently for each browser, there sadly is not a surefire answer to this question. It can vary from one WebDriver to the next. But the standard is the one posted above due to simplicity.

If you really want to determine the best implementation on a browser though the easiest way will be to determine what is fastest for JQuery or Javascript (Which you will likely find a lot of answers for). Chances are the implementation in the searching functions are extremely similar.

Some other important things to take into consideration when choosing the way that you decide to implement your selectors:

  1. Maintainability
  2. Readability
  3. Reliability
  4. Team Skill
  5. Frequency of change
1

I would go with the answers given here https://stackoverflow.com/questions/16788310/what-is-the-difference-between-css-selector-xpath-which-is-betteraccording-t?rq=1

There is a lot of conflicting information about this on the web, and it is a great pity that the Selenium website itself is not sharing some thoughts on this topic. I suppose that it is always a struggle to find the time to make better documentation.

  • Sadly this has little to do with Selenium itself and is based more on the Browser(s) and their Javascript implementation. When you trigger a find in Selenium it simply sends the command into the WebDriver which sends into the browser's driver which, typically, will execute it using Javascript. – Paul Muir Nov 4 '16 at 12:11
  • Thanks for this information Paul. I suppose it should be to the producers of the drivers to then detail which is the best strategy per their browser - if indeed they can clearly say that there is a best strategy. – SlightlyKosumi Nov 4 '16 at 12:22
1

According to this Presentation the best locators in terms of speed and stability are (in this order) ID, CLASS, CSS Selectors, XPath. As can be sen on the slides, Css Selectors are usually faster than xpath selectors. For me personally Xpath apears to be more easily readable, but I'm also trying to use Css Selectors over Xpath when neither Id nor Classes can be used

Not the answer you're looking for? Browse other questions tagged or ask your own question.