Simple strategy for choosing Selenium locators
One of the things an inexperienced test automation engineer struggle with when it comes to UI Test Automation is “what kind of Selenium locator I should use for a specific element”.
Note: I am no longer publishing professional posts on Medium, so this one may be outdated. Please check my blog at Hashnode: https://testclub.hashnode.dev/
Element locators that generated by tools usually are sub-optimal and a friend of mine Dmitry Rak has suggested a great locator selection strategy:
- locate by id (if it is not generated automatically)
- locate by name (which should be unique for a specific form)
- locate by css (for all other cases except text)
- xPath as a last choice (for all other complex cases and text search)
My personal preference is to insist on having a unique, non-generated id for each element we’re interested in at least in a test environment.
If you speak Russian — feel free to check the original post: https://lnkd.in/d55W2pX
PS
Did you enjoy this post? I think you might also find this video useful: https://www.youtube.com/watch?v=RVel7MZaSA0
It explains why test automation cannot fully replace testing.