Skip to main content
Version: 1.22

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.

var locator = page.Locator("text=Submit");
await locator.ClickAsync();

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.

var locator = page.Locator("text=Submit");
await locator.HoverAsync();
await locator.ClickAsync();

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.
await page.Locator("text=Sign up").ClickAsync();

// Find by CSS.
await page.Locator("button.sign-up").ClickAsync();

// Find by test id.
await page.Locator("data-testid=sign-up").ClickAsync();

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:
await page.Locator("button").ClickAsync();

// Works because we explicitly tell locator to pick the first element:
await page.Locator("button").First.ClickAsync();

// Works because Count knows what to do with multiple matches:
await page.Locator("button").CountAsync();

Lists

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

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

// Pattern 1: use locator methods to calculate text on the whole list.
var texts = await rows.AllTextContentsAsync();

// Pattern 2: do something with each element in the list:
var count = await rows.CountAsync()
for (let i = 0; i < count; ++i)
Console.WriteLine(await rows.Nth(i).TextContentAsync());

// 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
var texts = await rows.EvaluateAllAsync("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.

await page.Locator("button", new PageLocatorOptions { HasText = "Sign up" }).ClickAsync();

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 PageLocatorOptions { Has = page.Locator("button.subscribe") })

You can also filter an existing locator with Locator.Filter(options) method.

var buttonLocator = page.Locator("button");
// ...
await buttonLocator.Filter(new LocatorFilterOptions { HasText = "Sign up" }).ClickAsync();

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.

var handle = await page.QuerySelectorAsync("text=Submit");
await handle.HoverAsync();
await handle.ClickAsync();

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.

var locator = page.Locator("text=Submit");
await locator.HoverAsync();
await locator.ClickAsync();