Categories
Programming

Xpath vs CSS Selectors

After researching and reviewing on whether to choose XPATH or CSS selector, the following are the points I came across.

 

Some points to CSS

Generally CSS is easy to use and readable over XPATH.

REF: http://www.seleniumhq.org/docs/appendix_locating_techniques.jsp#starting-to-use-css-instead-of-xpath

 

CSS is native to browsers and XPATH is not

REF: http://old.eurostarconferences.com/blog/2013/1/28/liveblogged!-automated-web-testing-traps-for-the-unwary-with-simon-stewart

CSS is preferred for testing as it is having more advantages over XPATH

REF: http://seleniumeasy.com/selenium-tutorials/css-selectors-tutorial-for-selenium-with-examples

 

Some points to XPATH

XPATH can travese up the document from a child element to parents.

REF: http://www.rapidprogramming.com/questions-answers/difference-between-css-and-xpath-css-vs-xpath-1388

 

Speed wise CSS and XPATH can be equal, or either one would be a bit speedier than other.

Therefore speed comparison can be ignored. However off the record, I have encountered that most mention that CSS is much faster than XPATH.

REF: http://elementalselenium.com/tips/33-xpath-vs-css-revisited

 

Conclusion

If we are down to select either CSS or XPATH reference, as I see we should compare the generated CSS and XPATH and select the better readable and short path locating string. Others comments are highly appropriated.

 

Additional Notes

Converting XPATH to CSS seletors: Following article layout on how to change XPATH element reference to CSS.

https://saucelabs.com/resources/selenium/css-selectors

 

Differences between CSS and XPATH

Ref: http://www.rapidprogramming.com/questions-answers/difference-between-css-and-xpath-css-vs-xpath-1388

 

A general guideline on how to select elements in a preferred order (where ID is the king)

https://blog.mozilla.org/webqa/2013/09/26/writing-reliable-locators-for-selenium-and-webdriver-tests/

https://thenewcircle.com/static/bookshelf/selenium_tutorial/locators.html

A video presentation on how why CSS is preferred on many occations and comprehensive presentation on CSS vs XPATH for Selenium.

https://www.youtube.com/watch?v=6vPu3TO6XZ4

 

0 0 vote
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments