Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why one should prefer using CSS over XPath in IE?

I am working on an application which is compatible only with IE7 and IE8. I didn't know why but some suggested to use CSS over XPath while identifying the elements in IE. When I visited the official Selenium site. I read the message

WebDriver uses a browser’s native XPath capabilities wherever possible. On those browsers that don’t have native XPath support, we have provided our own implementation. This can lead to some unexpected behaviour unless you are aware of the differences in the various xpath engines.

I would like to know where I can find the differences in the various xpath engines and in which situations I should use CSS and and in which XPath specifically if I'm using IE. Thanks.

like image 520
Code Enthusiastic Avatar asked Dec 20 '12 15:12

Code Enthusiastic


2 Answers

According to Ashley Wilson's report from sauce labs:

Pros:

  1. They’re faster
  2. They’re more readable
  3. CSS is jQuery’s locating strategy
  4. No one else uses XPATH anyways!

It's a bit outdated, however here are the numbers:

image from cause lab's web site

Personally, I would argue about (2)-nd statement, I must agree with the rest.

Cons:

  1. No "bottom up" navigation. XPath has elem\..\another_elem
  2. Is Sizzle injected? Or Browser's CSS locator parser is used? Depends on the browser, and there are inconsistencies.
like image 116
Alex Okrushko Avatar answered Sep 30 '22 20:09

Alex Okrushko


The biggest reason for suggesting CSS selectors over XPath in IE is performance. IE does not provide a native XPath-over-HTML option as does Firefox and Chrome. It does, however, provide a native CSS selector engine, which will always be faster than the JavaScript-only XPath engine implementation used in the IE driver. And the performance comparison isn't even close. I've seen it measured as much as an order of magnitude slower for XPath locators than CSS selectors (though I can't find the citation at the moment). This is particularly true in versions of IE before 9, where the IE JavaScript engine was vastly slower than the Chakra JavaScript engine introduced in 32-bit IE9.

like image 43
JimEvans Avatar answered Sep 30 '22 21:09

JimEvans