0

I use find element by xpath to locate elements and find it useful but it is suggested to use find element by css when using Selenium Webdriver. Would like to know which one is better?

karshitbha
  • 319
  • 1
  • 2
  • 7
  • 1
    You can find some explanation here as why CSS is better http://stackoverflow.com/questions/16788310/what-is-the-difference-between-css-selector-xpath-which-is-betteraccording-t – Jyothishwar Deo Mar 05 '16 at 05:13

1 Answers1

1

As with anything and everything, when it comes to such choices, things are subjective. Following are the points that I would like to convey from my own knowledge.

  • CSS is faster compared to Xpath
  • CSS has a better cross browser compatibility
  • CSS is easy to understand, learn, implement
  • Xpath is a bit slow compared to CSS
  • Xpath implementation in each browser might differ giving rise to cross browser issues
  • Xpath might not work properly with old version of IE
  • Xpath is difficult to understand and implement

From the above points it is clear that CSS is better than Xpath. BUT a question might arise, if CSS is better than Xpath, why is Xpath still present and used widely.

The answer to that big BUT, AFAIK, is because, Xpath is more powerful than CSS. I have faced situations where certain element selections was possible using only Xpath and not CSS. For e.g.,

  • selecting elements based on text
  • parent/child/sibling selections
  • index based selection (Refer CSS Index Based Selection ) etc.

Hope this gives you some idea on where to use CSS and where to use Xpath. :)

Community
  • 1
  • 1
Keerthi Kumar P
  • 1,554
  • 9
  • 14