What is the Difference Between CSS and XPATH



  • 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?



  • 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



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2