As the other answer already said, the preferred order is this:
- ID Locator
- NAME Locator
- CSS Locator
- XPATH Locator
However, the reason why is simple: an
ID is supposed to be unique, so once you have a certain element with a specific identifier, this is unlikely to change. Also, no matter where the element is moved to on a page, the
ID stays the same.
However, an XPath is more brittle: for example, if elements switch places in a
body container (due to new elements being added, ...), the xpath might break.
Of course, you'll often have to rely on XPath or CssSelectors for dynamic or complex HTML.
Conclusion: try to get the developers to add as much
name elements as possible.