Google+

20. Different Types of Locators to identify UI elements


Selenium uses different types of locators to identify the UI elements on the page.

The following is the list of locators based on the priority and recommendations to use:

1. ID
2. Name
3. Class Name
4. Link
5. CSS
6. XPath

i.e. Theoretically, you have to follow the below steps while finding the Locator for the selected UI element:
  1. Locate the UI element by using its Unique ID else you have to go to the next step
  2. Locate the UI element by using its Unique Name else you have to go to the next step
  3. Locate the UI element by using its Unique Class Name else you have to go to the next step
  4. Locate the UI element by using its Unique Link else you have to go to the next step
  5. Locate the UI element by using CSS selector locator else you have to go the next step
  6. Locate the UI element by using its XPath locator
Practically, most of the people will try to locate the UI element using a unique ID. If  unique ID value is not available, they will use CSS selector locator followed by XPath locator.

Why different types of locators are used instead of having only one kind of locator to identify the UI elements ?

While building the application code, it is seen as a good practice to make sure that every element you need to interact with has an ID attribute and a Name attribute. Unfortunately, developers may not have provided ID or Name attributes to some elements in your application. So identifying those kind of elements may not be possible if we don't use the other type of locators like XPath or CSS selector locators.




Please comment below to feedback or ask questions.

How to find out the locators using the Selenium will be explained in the next post.



3 comments:

Unknown said...

Hi Arun ,
Very Useful and helpful post..
Thank you !
Learn a lot on Selenium.!!

Unknown said...

Thank you Arun ... your blog is very nice and easy to understand.
I am enjoying selenium learning.

Rohit Bhole said...

You have arrange all things very well.