3

Using Selenium WebDriver, while finding an element in HTML, why are XPaths considered to be slower than CSS selectors? I generally use XPaths and dynamic XPaths.

Can anyone logically explain me in detail?

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
naazneen3264
  • 315
  • 2
  • 7
  • 22
  • 1
    http://elementalselenium.com/tips/32-xpath-vs-css – Shubham Jain Dec 29 '15 at 11:42
  • They are considered, because (in general) they are slower. This was true especially in the **old** versions of **Internet Explorer**, but now the difference is not so relevant. Sometimes XPath is quite faster. See http://elementalselenium.com/img/chart-chrome.png (Chrome 32 - 2014) , http://elementalselenium.com/img/chart-firefox.png ( Firefox 26 - 2014), http://elementalselenium.com/img/chart-ie10.png ( IE 10 - 2012), http://elementalselenium.com/img/chart-ie9.png ( IE 9 - 2011 ), http://elementalselenium.com/img/chart-ie8.png ( IE 8 - 2009 ). – ROMANIA_engineer Dec 29 '15 at 13:31
  • http://stackoverflow.com/a/14139380/2504101 – olyv Dec 30 '15 at 08:56

1 Answers1

2

While this is impossible to answer precisely except for specific test cases, maybe a general answer can help a little. CSS is a rather constrained (though increasingly expressive, with more recent CSS versions) expression language that allows the evaluation of CSS selectors to be easily optimized. XPath, on the other hand, is a generalized and complex expression language, that allows much more selections than CSS, and thus is harder to optimize. So while in theory for equivalent XPath/CSS expressions a perfectly optimizing XPath engine should be able to achieve the same performance as an CSS engine, in practice in many cases XPath optimization will be sup-optimal, and thus CSS will be faster.

dret
  • 531
  • 3
  • 7