Hello Ushma, the primary difference between XPath and CSS Selectors is that, with the XPath we can traverse both forward and backward whereas CSS selector only moves forward. Although CSS selectors perform far better than Xpath and it is well documented in Selenium community. It has following advantages over XPath as:
-
Xpath engines are different in each browser, hence make them inconsistent
-
IE does not have a native xpath engine, therefore selenium injects its own xpath engine for compatibility of its API. Thus losing the advantage of using native browser features.
-
Xpath tend to become complex and hence make hard to read.
However there are some situations where, you need to use xpath, like while searching for a parent element or searching element by its text.
Hope this helps!
Enroll for the Selenium Course to become the certified tester.