Skip to main content


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, **kwargs) method.

locator = page.locator("text=Submit")

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 = page.locator("text=Submit")

Creating Locators

Use page.locator(selector, **kwargs) 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.

# Find by test id.


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:

# Works because we explicitly tell locator to pick the first element:
page.locator('button') # ⚠️ using first disables strictness

# Works because count knows what to do with multiple matches:

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.


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

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

# Pattern 1: use locator methods to calculate text on the whole list.
texts = rows.all_text_contents()

# Pattern 2: do something with each element in the list.
count = rows.count()
for i in range(count):

# 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.
texts = rows.evaluate_all("list => => 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", has_text="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", has=page.locator("button.subscribe"))

You can also filter an existing locator with locator.filter(**kwargs) method, possibly chaining it multiple times.

row_locator = page.locator("tr")
# ...
.filter(has_text="text in column 1")
.filter(has=page.locator("tr", has_text="column 2 button"))

Locator vs ElementHandle


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.

handle = page.query_selector("text=Submit")

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 = page.locator("text=Submit")