Updated: Jan 18, 2022
As I shared and demonstrated in my previous articles, there are just too many selectors that we can use to locate elements (e.g., By ID, Class Name, CSS...). And the question remains which one we should use and when.
The answer is straightforward and not complicated. If you can use the element ID or Name, make sure to use it as they are at the highest in the priority chain. After the name and Id come the class name.
The Class name is most commonly used if you want to target multiple elements that go by the same class. Last, if none of the options above are available, it's time to use the XPath and CSS locators you should only use if there is no other option.
To ensure that elements are located by their ID and names, you should always ensure that they received these elements when developed. This is the only way to ensure that the most stable and efficient locators locate elements.