How To Choose Correct Locator In Selenium?

Share and Enjoy !

Finding objects or elements in Webpages is the main job while coding with Selenium.You can get Locators from here by which you can uniquely locate your elements in webpage.

This post talk about the priority of choosing the locator. You can think about mandatory and assistive properties to uniquely identify an object in Webpages.

The different locator can be..

  • ID
  • Name
  • Class Name
  • CSS
  • Index
  • Tag Name
  • Link Text
  • Partial Link Text
  • xpath
  • Javascript
  • etc
Here is a pyramid or thumb rule to determine the mandatory properties to select locators that can uniquely determine an element in the webpage.

The thumb rule says:

Try to get id/Name first to identify an object using developer console. If developer has not provided these then go for Secondary locator that is Xpath,CSS,Index etc to identify an object or element uniquely. If you fail to get first 2 then go with Composite locator or assistive locator  to determine an object or element uniquely.
Let me know if you like the post. Please share this if you liked it. Also please feel free to comment.

Share and Enjoy !

Leave a Reply

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