CSS Selectors vs XPath

CSS Selectors vs XPath

Advantages Of XPath

  • Allows you to navigate up the DOM when searching for elements. E.g. You can search based on a parent element with XPath expressions but not CSS Selectors.

  • Can be used with old browsers some of which are still in use by some companies.

  • Greater flexibility than CSS Selectors.

Advantages of CSS Selectors

  • Faster than XPath in most cases.

  • Considered easier to learn and implement by many.

Summary

The choice is generally down to personal preference. I prefer to use XPath as I personally prefer the path structure to CSS Selectors and with a background in IT I found the difference in time to learn and implement either one is negligible if you are dedicated to making tests, i.e. if it is your job.

Even on a performance level the difference in speed is often massively overshadowed by other factors like the speed of the machine when using different browser drivers and the latency between the computer running the tests and the environment being tested. These differences in time can build up if you need to run thousands of test cases, but for a typical tester best option is to choose the one that you are most comfortable with. If you spend less time having to google how to use your location strategy then you will be writing more tests per day.

So in conclusion you should try using both and choose your favourite. If you are required to write extremely efficient tests, then it might be best to read up about which location strategy is the most efficient for your testing scenario.

Leave a reply

Your email address will not be published. Required fields are marked*

CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.