Which Locator To Use? Read This Post.

Hello Folks,

We have seen eight types of locators in Selenium Webdriver. Now we may confuse which locators we should use. It is frequently asked interview question as well. So, we will learn the orders we should use locators.

Selenium web driver takes time to find web element which is more or less based on type of locator we used. If we use slow locators, it will increase test execution time as well. So, we must use correct and faster locators.

“ID” locator is faster among all locators. So, if any web element has ID as an attribute we must use ID locator. ID should be unique in a web page. Even Page Object Model(POM) has advantages when We use ID locators. We will see it later. ID locator is faster because at its roots, it calls “document.getElementById()”(A javaScript syntax) which is very much optimized by many browsers. Even ID helps in finding web element uniquely.

“Name” locator comes after ID. If any web element has not ID attribute, we can use name attribute if available. Even Page Object Model(POM) has advantages when We use name locators. But it may not identify web element uniquely has multiple web elements can share same name. We can ask developers to provide either ID or Name for every web element to faster test script execution.

Locators like ClassName, TagName, LinkText and PartialLinkText are mostly used based on situation. ClassName and TagName can be used to locate a list of web elements while LinkText and PartialLinkText can be used for anchor tag web elements.

“CSS Selector” is best option if web element has no ID and name. It is faster. It also improves the performance. It is very compatible across browsers. It is very useful when we want to test our application on multiple browsers because CSS engine are consistent in all browsers. CSS is best for IE as XPath does not work in IE always. But writing CSS is not simpler than XPath however it is more readable than XPath.

We should use XPath as a last resort to locate a web element. It is slowest among all locators. But it provides you reliable ways to locate web elements. XPath engines are different in each browser, hence make them inconsistent across browsers. That means if you write XPath for your application in Chrome browser, it may not work on IE.

Difference between XPath and CSS:

  1. CSS is faster than XPath.
  2. CSS is more consistent across browsers than XPath. CSS is useful for cross browser testing.
  3. CSS is more readable than XPath.
  4. CSS can only traverse down the DOM while XPath can traverse DOM up and down.
  5. CSS improves the performance because it is faster.
  6. CSS is much better for IE than XPath.
  7. CSS has deprecated contains method while XPath provides which is very useful in locating web elements.
  8. CSS is complex and difficult to write compare to XPath.

So, we can define order as below:

ID, Name, CSS , XPath.

That’s it. I hope you should be clear which locators should be used. Now you can give answer of interview question as well. If you have any doubt, please comment or send out an email through contact page.

If you like my posts, please like, comment and share. Feedback and suggestions are always welcomed.

#HappyLearning

Author: Amod Mahajan

My name is Amod Mahajan and I am an IT employee with 4+ years of experience in Software testing and staying in Bengaluru. My area of interest is Automation testing. I started from basics and went throw so many selenium tutorials. Thanks to Mukesh Otwani as his tutorials are easy and cover basics to advance. I have habit of exploring concepts by deep diving. I used to make notes. I thought of sharing my knowledge through posts and now I am here. #KeepLearning #ShareLearning