There are 8 locators in selenium. Those are…
- tagName
- id
- name
- className
- linkText
- partialLinkText
- cssSelector
- xpath
It is confusing to decide which locator to select while identifying the element. To avoid the confusion below are the best practices while selecting the locators.
- First preference should be given to tagName.
- If tagName is not unique or not available then next preference should be given to id.
- If id is not unique or not available then next preference should be given to name
- If name is not unique or not available then next preference should be given to linkText
- Next preference would be className and xpath respectively.
- cssSelector is rarely used because all webElement cannot be identified using cssSelector. We can identify any web element using xpath.
- partialLink text is used if web element is link and text is half dynamic