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.

This question already has an answer here:

Difference Between CSS and XPATH ?

Which is the best locator in selecting Elements in selenium?

If Xpath is the better why? why should we use Xpath instead of CSS?

If CSS is the better why? why should we use CSS instead of Xpath ?

What are the advantages of both Xpath and CSS ?

What are the Disadvantages of both Xpath and CSS?

share|improve this question

marked as duplicate by Kate Paulk, Lyndon Vrooman, corsiKa Jul 14 at 19:17

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

2 Answers 2

TL;DR: Neither. Use ID or name.

I strongly disagree with @Jon Duffy. If your CSS designers are up to snuff, they will create custom class names for the elements, so even if elements are moved around (XPATH changes), the class name itself stays the same. Even if CSS classes have different definitions, the name itself is the same.

Also, location by CSS is faster and more reliable. All Selenium "best practices" guides I've seen so far advised to prefer CSS over XPATH. See link here. In all Selenium best practices I've see so far, XPATH is always a last resort locator.

Of course, best locator is ID or name.

Even if appearance or position of element may change, it's ID or NAME should stay the same. So if you use them as locators, your tests are robust.

CSS is faster and more reliable than XPATH at least on IE. Also, you want to run your test cross-browser, and IE will likely remain big part of target audience. So you need to talk to your developers and designers and explain them why for testing ID, names and CSS locators are preferable, so you can avoid the hassle of XPATH.

Writing reliable locators - by Mozilla Web QA team

share|improve this answer
    
I Guess i didn't explain fully, We create custom tags for the testing and target them with XPATH. This means there are no long / flakey XPATH selections. This also helps maintain clarity between design code and test code. XPATH is defiantly more powerful as a selection tool. As of speed... I don't know, I would be grateful if you had a link regarding this. We have gone through a complete front end redesign and the many of our tests were still functional afterwards as they were not tied to the CSS. –  Jon Duffy Jul 14 at 14:29

Css is liable to change; UI tweaks, making a site/app responsive etc.

By choosing to target elements XPATH the tags will likely stay around much longer and therefore requires less maintenance on your tests. If your site / app is generated using a framework you can have the XPATH targets generated in the back end and maintain some separation from the more changeable UI

Another point for generating XPATH in the back end. You may serve different CSS to different screen size, with XPATH these targets can remain the same across sites.

Edit We create customs tag that can be targeted with a simple XPATH expression, not good practice to have long complicated XPATH expression, this can only lead to flakey tests.

Edit 2

XPATH:

Pro's

  • powerful selection of the DOM

Con's:

  • no native support for xpath in IE (WebDriver uses a 3rd party library)

CSS:

Pro's

  • looks simpler
  • consistent support across browsers

Con's:

  • Need to get designers on board
  • Tests may need to be updated with UI changes

Conclusion

XPATH works for me as I have support of the developers, who will add custom tags for case where elements won't be unique (and therefore cannot select by ID) and I can add these tags to the code when needed. If out testers worked separately from the developers this would be a different story.

share|improve this answer

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