Skip to main content

Locators

Locators are the central piece of Playwright's auto-waiting and retry-ability. In a nutshell, locators represent a way to find element(s) on the page at any moment. Locator can be created with the Page.locator(selector[, options]) method.

Locator locator = page.locator("text=Submit");
locator.click();

Every time locator is used for some action, up-to-date DOM element is located in the page. So in the snippet below, underlying DOM element is going to be located twice, prior to every action. This means that if the DOM changes in between the calls due to re-render, the new element corresponding to the locator will be used.

Locator locator = page.locator("text=Submit");
locator.hover();
locator.click();

Creating Locators

Use Page.locator(selector[, options]) method to create a locator. This method takes a selector that describes how to find an element in the page. Playwright supports many different selectors like Text, CSS, XPath and many more. Learn more about available selectors and how to pick one in this in-depth guide.

// Find by text.
page.locator("text=Sign up").click();

// Find by CSS.
page.locator("button.sign-up").click();

// Find by test id.
page.locator("data-testid=sign-up").click();

Strictness

Locators are strict. This means that all operations on locators that imply some target DOM element will throw an exception if more than one element matches given selector.

// Throws if there are several buttons in DOM:
page.locator("button").click();

// Works because we explicitly tell locator to pick the first element:
page.locator("button").first().click(); // ⚠️ using first disables strictness

// Works because count knows what to do with multiple matches:
page.locator("button").count();
caution

Using Locator.first(), Locator.last(), and Locator.nth(index) is discouraged since it disables the concept of strictness, and as your page changes, Playwright may click on an element you did not intend. It's better to make your locator more specific. Learn more below in Filtering Locators and the selectors guide.

Lists

You can also use locators to work with the element lists.

// Locate elements, this locator points to a list.
Locator rows = page.locator("table tr");

// Pattern 1: use locator methods to calculate text on the whole list.
List<String> texts = rows.allTextContents();

// Pattern 2: do something with each element in the list.
int count = rows.count()
for (int i = 0; i < count; ++i)
System.out.println(rows.nth(i).textContent());

// Pattern 3: resolve locator to elements on page and map them to their text content.
// Note: the code inside evaluateAll runs in page, you can call any DOM apis there.
Object texts = rows.evaluateAll("list => list.map(element => element.textContent)");

Filtering Locators

When creating a locator, you can pass additional options to filter it.

Filtering by text will search for a particular string somewhere inside the element, possibly in a descendant element, case-insensitively. You can also pass a regular expression.

page.locator("button", new Page.LocatorOptions().setHasText("Sign up")).click();

Locators also support an option to only select elements that have a descendant matching another locator. Note that inner locator is matched starting from the outer one, not from the document root.

page.locator("article", new Page.LocatorOptions().setHas(page.locator("button.subscribe")))

You can also filter an existing locator with Locator.filter([options]) method, possibly chaining it multiple times.

Locator rowLocator = page.locator("tr");
// ...
rowLocator
.filter(new Locator.FilterOptions().setHasText("text in column 1"))
.filter(new Locator.FilterOptions().setHas(
page.locator("button", new Page.LocatorOptions().setHasText("column 2 button"))
))
.screenshot();

Locator vs ElementHandle

caution

We only recommend using ElementHandle in the rare cases when you need to perform extensive DOM traversal on a static page. For all user actions and assertions use locator instead.

The difference between the Locator and ElementHandle is that the latter points to a particular element, while Locator captures the logic of how to retrieve that element.

In the example below, handle points to a particular DOM element on page. If that element changes text or is used by React to render an entirely different component, handle is still pointing to that very stale DOM element. This can lead to unexpected behaviors.

ElementHandle handle = page.querySelector("text=Submit");
handle.hover();
handle.click();

With the locator, every time the locator is used, up-to-date DOM element is located in the page using the selector. So in the snippet below, underlying DOM element is going to be located twice.

Locator locator = page.locator("text=Submit");
locator.hover();
locator.click();