Selenium - How to chose the right selector?

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 should we use and when.


The answer is actually very simple 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 which you should only use if there is no other option.


To ensure that elements are located by their ID and names, you should always make sure that they are receiving these element attributes when they are developed. This is the only way to ensure that elements are located by the most stable and efficient locators.



10 views0 comments