Skip to content

Make Selenium Easy

And Keep It That Way

  • Home
  • Share
  • Toggle search form

Which Locator To Use? Read This Post.

Posted on 02/19/2025 By admin

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

Uncategorized

Post navigation

Previous Post: TestNG Tutorials 17: Can @Test Annotation Be Used For A Class In TestNG?
Next Post: Amod Mahajan

Related Posts

Part 5: Waits In Selenium: What Happens When We Mix Sleep With Other Types Of Waits ? Uncategorized
REST Assured Tutorial 6 – Interface in OOPS – Implement As You Wish Uncategorized
How To Upload Multiple Files In Selenium WebDriver – Java Uncategorized
September 15, 2018 – Make Selenium Easy Uncategorized
skyscreamer jsonassert tutorials Uncategorized
Postman Tutorial Part 16 – Introduction To Collections In Postman Uncategorized

Recent Posts

  • Getting Started with Selenium 4: What Is New and How to Upgrade from Selenium 3
  • Manual Testing
  • Baby Steps To Become Efficient Selenium-Java Automation Tester
  • Features of Selenium 4.0.0 Release – Java Binding
  • Part 1: Handling Drop-down Created Using SELECT Tag In Selenium

Recent Comments

No comments to show.

Archives

  • April 2026
  • April 2025
  • March 2025
  • February 2025
  • January 2025
  • December 2024
  • November 2024
  • October 2024
  • September 2024
  • August 2024
  • April 2024
  • March 2024
  • February 2024
  • December 2023
  • October 2023
  • August 2023
  • November 2022
  • September 2022
  • August 2022
  • July 2022
  • May 2022
  • March 2022
  • October 2021
  • April 2021
  • March 2021
  • January 2021
  • December 2020
  • October 2020
  • September 2020
  • August 2020
  • June 2020
  • May 2020
  • April 2020
  • March 2020
  • February 2020
  • January 2020
  • December 2019
  • November 2019
  • October 2019
  • September 2019
  • August 2019
  • May 2019
  • December 2018
  • November 2018
  • October 2018
  • September 2018
  • August 2018
  • July 2018
  • January 2018

Categories

  • Getting Started
  • Uncategorized

Copyright © 2026 Make Selenium Easy.

Powered by PressBook Masonry Dark