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”.

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: