DEV Community

Cover image for What Are Locators in Automation?
WallTech
WallTech

Posted on

What Are Locators in Automation?

In test automation, locators are essential. They help your autotest find and interact with elements on a web page - like buttons, input fields, links, and more.

A locator is a way to uniquely identify a web element within the DOM (Document Object Model).

Think of it as an address for the element you want your test to interact with - whether it's clicking a button or entering text into a field.

Selector vs Locator - What's the Difference?

  • Selector: The actual string or pattern used to identify an element (e.g., CSS selector, XPath).
  • Locator: The abstraction or method that uses a selector to find the element in automation frameworks.

In short:
Selector = the “how”
Locator = the “tool” that uses the selector

Examples in Playwright:
// Using a CSS selector
await page.locator('button.login').click();

// Using a text selector
await page.locator('text=Submit').click();

Why Locators Matter

✔️ Stable locators = reliable tests
❌ Bad locators = flaky tests

Choosing the right locator strategy is key to building robust and maintainable automation!

Follow our social media profiles so you don’t miss new updates: LinkedIn | Dev.to | Medium | Walltech

Top comments (0)