TestProject Forum
Powered by leading experts in the test automation community

Preference Of Element Selectors

Hi Everyone. This question is a general question to everyone and I need this answer for my understanding. We know that the preference of an element selector starts with id, name…and goes up to CSS selector and finally ends at XPath.
May I know why is the XPath used most frequently and why not the CSS selector as per the industry practice?
Also regarding test project why does it uses XPath as the primary element selector and not the CSS selector.

Hi @navnish.chaudhury,

XPath is a more robust solution and is used more often for several reasons. For example, with XPath one can navigate to parent elements (up the DOM) if the targeted element has nothing unique in it. In addition, XPath has some extra methods that can be used that are not available in CSS selectors like starts-with, contains, substring, etc. which allow you to generate more complex selectors. A more simple reason is being that XPath is for XMLs in general and more people are familiar with it. If we are looking at performance, the difference is usually in microseconds so it is not a good enough reason use CSS selectors if you already have and/or familiar with XPath.

As for TestProject, I am not sure what you are referring to. The AI powered web recorder will choose CSS selector as it’s first strategy if available. In case it cannot find anything unique enough for CSS, it will generate smart XPath selectors which are optimized and not just the absolute path of the targeted element. Having said that, TestProject recorder is constantly evolving thanks to the many valuable inputs from the community and it gets better and smarter everyday.

For reference, I’m attaching a quick recording that demonstrates the CSS selector strategy being the preferred one.

Hope this is what you were looking for.

Thanks Eldar for responding explaining with some proper examples, and yes this indeed helped.