I have heard that, when using Selenium, the CSS Locator has better performance than the XPath Locator. Which Locator do you use in your tests? Have you seen a great performance improvement when using CSS Locators? Were there times when you had to use XPath instead of the corresponding CSS Locator?
- Anybody can ask a question
- Anybody can answer
- The best answers are voted up and rise to the top
Don't forget the not only performance is better with CSS locators, it's the compatibility too that matters.
We are testing on a multy browser environment in which we use: IE, SAFARI, FIREFOX, CHROME.
On IE the xpath almost never works OR it is SO slow that it can't be managed. So we use CSS where ever we can. Unfortunately IE does not support many CSS logics like, previous item, next item, counters and so on. But that can be arranged...
You have to tell your Developers to give distinctive IDs To every and each element you are using. It will greatly speed up your performance because you wont be needing to much XPATH magic to reach elements.
CSS is better with IE that's for sure. On other browsers I didn't really spot any difference.
Well, in fact I am using xpath. The best way is to put an static -of course unique- id to the elements you want to refer.
An example of something you can only do in XPATH is go the parent of the current node. So while I recommend using CSS when you can, sometimes XPATH is the only way.
Edit : Actually, brain-fart on my side. The following site has two very useful charts that compare CSS and XPATH locators if those exist plus DOM locators for good measure, all with special notice to Selenium : http://www.simple-talk.com/dotnet/.net-framework/xpath,-css,-dom-and-selenium-the-rosetta-stone/
Very useful stuff.
In my experience xpath can be very flaky as it relies on the current structure of the UI for navigation. Change the page structure and whoosh, there go your tests.
For that reason, I prefer the stability of css selectors.