Take the 2-minute tour ×
Software Quality Assurance & Testing Stack Exchange is a question and answer site for software quality control experts, automation engineers, and software testers. It's 100% free, no registration required.

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?

share|improve this question

4 Answers 4

up vote 6 down vote accepted

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.

So conclusion:

CSS is better with IE that's for sure. On other browsers I didn't really spot any difference.

share|improve this answer
    
"IE does not support many CSS logics like, previous item, next item, counters and so on. But that can be arranged... " How do you arrange? –  Tarun May 6 '11 at 8:44
    
Yeah, sorry.. arranged is continued on the below line. You have to tell your... etc,etc. But if needed in critical parts you still can use XPATH, just ceep it to a minimum. :) –  Hannibal May 6 '11 at 10:37
1  
Saying that CSS is the best locator strategy in the first half of your answer, and then saying that to get it to work you need to get your devs to modify the source code in the secnd half surely shows that CSS is not the best in all cases. Whilst it can be slow on IE XPath requires not modification from devs to allow you to find your element, so you could in effect say XPath is better as a locator strategy unless you are doing primarily IE testing. Not a great answer IMHO. –  Ardesco Jul 1 '11 at 14:03
    
I agree that XPATH is not so great on IE. But I would argue with not asking devs to add some distinctive IDs. How do you use xpath? Do you find elements (with xpath) by their position in DOM? I would do everything I could to get distinctive IDs instead. –  yoosiba Jul 7 '11 at 14:12

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.

share|improve this answer
    
agreed but most of the times you get an application which does not have such testing hooks and you need to make you scripts work and find defects –  Tarun May 6 '11 at 8:57
    
I know. We have an application which generates dynamic ids. I use different plugins for such as firebug and firefinder under Firefox. Another trick is to use a style class name which is unique for the element you are looking for. –  Luixv May 6 '11 at 9:05
    
even style class is duplicated at times –  Tarun May 6 '11 at 9:10

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.

share|improve this answer

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.

share|improve this answer
1  
I disagree, CSS locator also depends on current structure of UI for navigation. If UI changes CSS locator should also be broken. –  Tarun May 6 '11 at 10:13
    
I agree with @CBA whole completely. We had originally used xpath for all of our selectors, and it quickly became apparent that using the class_name or id was the only way to write the tests in such a way that they would not need to be fixed for any small change to the html –  Jason Ward May 6 '11 at 14:26
1  
xpath and css have almost same probability of breaking if the UI changes. Both depend on the current structure of UI. If UI elements are well identified i.e. can be uniquely identified with one or a combination of attributes, both xpath and css can be created in a very reliable way. If either of them is not created from the root, both can survive a few UI changes. –  Suchit Parikh May 6 '11 at 17:04
    
@Jason I would suggest that you were using poorly constructed XPath's then, a well formed Selenium XPath will ne no more brittle than a CSS locator. –  Ardesco May 18 '11 at 12:50
    
I think there is confusion here between two independent interpretations of the question: 1) by which means to identify elements (ids, classes, or DOM paths) and 2) which language (and locator engine) to use, XPath or CSS –  reinierpost Sep 19 '12 at 14:57

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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