Home > Automation Testing > Types of locators and best practices to use locators in selenium

Types of locators and best practices to use locators in selenium

There are 8 locators in selenium. Those are…

  1. tagName
  2. id
  3. name
  4. className
  5. linkText
  6. partialLinkText
  7. cssSelector
  8. 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.

  1. First preference should be given to tagName.
  2. If tagName is not unique or not available then next preference should be given to id.
  3. If id is not unique or not available then next preference should be given to name
  4. If name is not unique or not available then next preference should be given to linkText
  5. Next preference would be className and xpath respectively.
  6. cssSelector is rarely used because all webElement cannot be identified using cssSelector. We can identify any web element using xpath.
  7. partialLink text is used if web element is link and text is half dynamic
This Article is TAGGED in , , . BOOKMARK THE permalink.

Avatar photo
Amit Bharamasali
Automation Software Test Engineer. Skills: Manual Testing, Selenium, Java, Protractor, JavaScript, Katalon Studio, Groovy, Appium, Sahi, Sahi Script and UI/UX testing.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">