I've read plenty of forums and blogs but somehow I ain't convinced on this yet. Recently a friend of mine was asked this question in an interview.
1 Answers
ID: id will obviously be the most preferred attribute to select an element (unless it is generated). It is also the fastest way to locate an element since the id is unique.
Name / tagName: This locator works well for finding multiple elements with the same name.
Linktext / PartialLinkText - Locator is good for navigation activites.
CSS vs Xpath -
You may go through this question for the CSS vs XPath debate. As per the answers, CSS is more readable, faster (especially in IE lower versions), and will work in all browsers.
A big plus with Xpath is that it supports 'contains' whereas it is deprecated in CSS (atleast in Selenium). But, 'contains' works like a charm with the Jsoup library and I preferred using Jsoup(CSS selector) over Xsoup(Xpath selector) for selecting elements in the document.
As far as Selenium is concerned, many prefer to use XPath (atleast this is what I've observed), but it has to be well formed so that it doesn't change a lot when web page changes.
Another good article would be this - http://elementalselenium.com/tips/32-xpath-vs-css

- 1
- 1

- 2,496
- 1
- 21
- 33