
Selectors Tell, Locators Do -- Stop Mixing Them Up in Automation!

If you’re into automation with tools like Selenium, Playwright, or Cypress, you’ve probably heard both terms — locator and selector.
They sound similar, but they’re not the same thing. Knowing the difference can make your automation scripts cleaner, faster, and easier to maintain.
1. What is a Selector?
A selector is simply a string or expression used to find an element in the HTML DOM.
Think of it like an address for an element — but just the raw address, nothing more.
Example -
// CSS selector - just a string
String buttonSelector = "button#submit";
// XPath selector - also just a string
String linkSelector = "//a[text()='Login']";
Here, "button#submit" or "//a[text()='Login']" are just selectors. They can’t interact with elements directly — they’re just telling the automation tool where to look.
2. What is a Locator?
A locator is an object provided by automation frameworks that wraps a selector and gives you handy methods to find and interact with elements.
It’s like having the address + a built-in remote control for that element.
Example -
Locator submitButton = page.locator("button#submit");
submitButton.click(); // Interact directly
## Here:
- "button#submit" → selector
- page.locator(...) → locator (framework’s way of using that selector)
The locator knows:
- When the element appears
- How to retry if it’s not ready
- How to perform actions (click, fill, hover) safely
3. Boost Efficiency with Locator Objects
If you directly use selectors every time, you might end up writing:
page.click("button#submit");
This works, but:
- You repeat the selector multiple times
- If the selector changes, you update it everywhere
With locators:
Locator submitButton = page.locator("button#submit");
submitButton.click();
Now:
- The selector is defined once
- You can reuse it across actions
- The locator waits automatically for the element
4. Real-Life Analogy
- Selector → Just a home address written on a piece of paper
- Locator → A GPS system that not only finds the address but also drives you there safely, avoiding traffic