2021-12-08 20:54:01 +03:00
---
id: locators
title: "Locators"
---
2022-08-17 08:00:54 +03:00
[Locator]s are the central piece of Playwright's auto-waiting and retry-ability. In a nutshell, locators represent
2022-10-27 01:30:22 +03:00
a way to find element(s) on the page at any moment.
2021-12-08 20:54:01 +03:00
```js
2022-10-04 03:02:46 +03:00
const locator = page.getByText('Submit');
2021-12-08 20:54:01 +03:00
await locator.click();
```
```java
2022-10-04 03:02:46 +03:00
Locator locator = page.getByText("Submit");
2021-12-08 20:54:01 +03:00
locator.click();
```
```python async
2022-10-04 03:02:46 +03:00
locator = page.get_by_text("Submit")
2021-12-08 20:54:01 +03:00
await locator.click()
```
```python sync
2022-10-04 03:02:46 +03:00
locator = page.get_by_text("Submit")
2021-12-08 20:54:01 +03:00
locator.click()
```
```csharp
2022-10-04 03:02:46 +03:00
var locator = page.GetByText("Submit");
2021-12-08 20:54:01 +03:00
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.
```js
2022-10-04 03:02:46 +03:00
const locator = page.getByText('Submit');
2021-12-08 20:54:01 +03:00
// ...
await locator.hover();
await locator.click();
```
```java
2022-10-04 03:02:46 +03:00
Locator locator = page.getByText("Submit");
2021-12-08 20:54:01 +03:00
locator.hover();
locator.click();
```
```python async
2022-10-04 03:02:46 +03:00
locator = page.get_by_text("Submit")
2021-12-08 20:54:01 +03:00
await locator.hover()
await locator.click()
```
```python sync
2022-10-04 03:02:46 +03:00
locator = page.get_by_text("Submit")
2021-12-08 20:54:01 +03:00
locator.hover()
locator.click()
```
```csharp
2022-10-04 03:02:46 +03:00
var locator = page.GetByText("Submit");
2021-12-08 20:54:01 +03:00
await locator.HoverAsync();
await locator.ClickAsync();
```
2022-08-17 08:00:54 +03:00
## Strictness
2021-12-08 20:54:01 +03:00
Locators are strict. This means that all operations on locators that imply
2022-04-25 22:06:18 +03:00
some target DOM element will throw an exception if more than one element matches
2022-10-27 01:30:22 +03:00
given selector. For example, the following call throws if there are several buttons in the DOM:
2021-12-08 20:54:01 +03:00
```js
2022-10-04 03:02:46 +03:00
await page.getByRole('button').click();
2021-12-08 20:54:01 +03:00
```
```python async
2022-10-04 03:02:46 +03:00
await page.get_by_role("button").click()
2021-12-08 20:54:01 +03:00
```
```python sync
2022-10-04 03:02:46 +03:00
page.get_by_role("button").click()
2021-12-08 20:54:01 +03:00
```
```java
2022-10-04 03:02:46 +03:00
page.getByRole("button").click();
2021-12-08 20:54:01 +03:00
```
```csharp
2022-10-04 03:02:46 +03:00
await page.GetByRole("button").ClickAsync();
2022-10-27 01:30:22 +03:00
```
2021-12-08 20:54:01 +03:00
2022-10-27 01:30:22 +03:00
On the other hand, Playwright understands when you perform a multiple-element operation,
so the following call works perfectly fine when locator resolves to multiple elements.
```js
await page.getByRole('button').count();
```
2021-12-08 20:54:01 +03:00
2022-10-27 01:30:22 +03:00
```python async
await page.get_by_role("button").count()
```
```python sync
page.get_by_role("button").count()
```
```java
page.getByRole("button").count();
```
```csharp
2022-10-04 03:02:46 +03:00
await page.GetByRole("button").CountAsync();
2021-12-08 20:54:01 +03:00
```
2022-10-27 01:30:22 +03:00
You can explicitly opt-out from strictness check by telling Playwright which element to use when multiple element match, through [`method: Locator.first`], [`method: Locator.last`], and [`method: Locator.nth`]. These methods are **not recommended** because when your page changes, Playwright may click on an element you did not intend. Instead, follow best practices below to create a locator that uniquely identifies the target element.
2022-07-21 03:09:14 +03:00
2021-12-08 20:54:01 +03:00
2022-09-30 21:14:13 +03:00
## Locating elements
2022-10-27 01:30:22 +03:00
Playwright comes with multiple built-in ways to create a locator. To make tests resilient, we recommend prioritizing user-facing attributes and explicit contracts, and provide dedicated methods for them, such as [`method: Page.getByText`]. It is often convenient to use the [code generator ](./codegen.md ) to generate a locator, and then edit it as you'd like.
```js
await page.getByText('Log in').click();
```
```java
page.getByText("Log in").click();
```
```python async
await page.get_by_text("Log in").click()
```
```python sync
page.get_by_text("Log in").click()
```
```csharp
await page.GetByText("Log in").ClickAsync();
```
If you absolutely must use CSS or XPath locators, you can use [`method: Page.locator`] to create a locator that takes a [selector ](./selectors.md ) describing how to find an element in the page.
Note that all methods that create a locator, such as [`method: Page.getByLabel`], are also available on the [Locator] and [FrameLocator] classes, so you can chain them and iteratively narrow down your locator.
```js
const locator = page.frameLocator('#my-frame').getByText('Submit');
await locator.click();
```
```java
Locator locator = page.frameLocator("#my-frame").getByText("Submit");
locator.click();
```
```python async
locator = page.frame_locator("#my-frame").get_by_text("Submit")
await locator.click()
```
```python sync
locator = page.frame_locator("my-frame").get_by_text("Submit")
locator.click()
```
```csharp
var locator = page.FrameLocator("#my-frame").GetByText("Submit");
await locator.ClickAsync();
```
2022-09-30 21:14:13 +03:00
2022-10-27 01:30:22 +03:00
### Locate by text using [`method: Page.getByText`]
The easiest way to find an element is to look for the text it contains. You can match by a substring, exact string, or a regular expression.
2021-12-08 20:54:01 +03:00
```js
2022-10-04 03:02:46 +03:00
await page.getByText('Log in').click();
2022-10-27 01:30:22 +03:00
await page.getByText('Log in', { exact: true }).click();
await page.getByText(/log in$/i).click();
2022-09-30 21:14:13 +03:00
```
```java
2022-10-04 03:02:46 +03:00
page.getByText("Log in").click();
2022-10-27 01:30:22 +03:00
page.getByText("Log in", new Page.GetByTextOptions().setExact(true)).click();
page.getByText(Pattern.compile("log in$", Pattern.CASE_INSENSITIVE)).click();
2022-09-30 21:14:13 +03:00
```
```python async
2022-10-04 03:02:46 +03:00
await page.get_by_text("Log in").click()
2022-10-27 01:30:22 +03:00
await page.get_by_text("Log in", exact=True).click()
await page.get_by_text(re.compile("Log in", re.IGNORECASE)).click()
2022-09-30 21:14:13 +03:00
```
```python sync
2022-10-04 03:02:46 +03:00
page.get_by_text("Log in").click()
2022-10-27 01:30:22 +03:00
page.get_by_text("Log in", exact=True).click()
page.get_by_text(re.compile("Log in", re.IGNORECASE)).click()
2022-09-30 21:14:13 +03:00
```
```csharp
2022-10-04 03:02:46 +03:00
await page.GetByText("Log in").ClickAsync();
2022-10-27 01:30:22 +03:00
await page.GetByText("Log in", new() { Exact: true }).ClickAsync();
await page.GetByText(new Regex("Log in", RegexOptions.IgnoreCase)).ClickAsync();
2022-09-30 21:14:13 +03:00
```
You can also [filter by text ](#filter-by-text ) when locating in some other way, for example find a particular item in the list.
```js
2022-10-27 01:30:22 +03:00
await page.getByTestId('product-item').filter({ hasText: 'Playwright Book' }).click();
2022-09-30 21:14:13 +03:00
```
```java
2022-10-27 01:30:22 +03:00
page.getByTestId("product-item").filter(new Locator.FilterOptions().setHasText("Playwright Book")).click();
2022-09-30 21:14:13 +03:00
```
```python async
2022-10-27 01:30:22 +03:00
await page.get_by_test_id("product-item").filter(has_text="Playwright Book").click()
2022-09-30 21:14:13 +03:00
```
```python sync
2022-10-27 01:30:22 +03:00
page.get_by_test_id("product-item").filter(has_text="Playwright Book").click()
2022-09-30 21:14:13 +03:00
```
```csharp
2022-10-27 01:30:22 +03:00
await page.GetByTestId("product-item").Filter(new() { HasText = "Playwright Book" }).ClickAsync();
2022-09-30 21:14:13 +03:00
```
2021-12-08 20:54:01 +03:00
2022-10-27 01:30:22 +03:00
### Locate based on accessible attributes with [`method: Page.getByRole`]
2021-12-08 20:54:01 +03:00
2022-10-27 01:30:22 +03:00
The [`method: Page.getByRole`] locator reflects how users and assistive technology percieve the page, for example whether some element is a button or a checkbox. When locating by role, you should usually pass the accessible name as well, so that locator pinpoints the exact element.
2022-09-30 21:14:13 +03:00
```js
2022-10-04 03:02:46 +03:00
await page.getByRole('button', { name: /submit/i }).click();
2022-09-30 21:14:13 +03:00
2022-10-04 03:02:46 +03:00
await page.getByRole('checkbox', { checked: true, name: "Check me" }).check();
2021-12-08 20:54:01 +03:00
```
```python async
2022-10-27 01:30:22 +03:00
await page.get_by_role("button", name=re.compile("submit", re.IGNORECASE)).click()
2022-09-30 21:14:13 +03:00
2022-10-04 03:02:46 +03:00
await page.get_by_role("checkbox", checked=True, name="Check me"]).check()
2022-09-30 21:14:13 +03:00
```
```python sync
2022-10-27 01:30:22 +03:00
page.get_by_role("button", name=re.compile("submit", re.IGNORECASE)).click()
2022-09-30 21:14:13 +03:00
2022-10-04 03:02:46 +03:00
page.get_by_role("checkbox", checked=True, name="Check me"]).check()
2022-09-30 21:14:13 +03:00
```
2021-12-08 20:54:01 +03:00
2022-09-30 21:14:13 +03:00
```java
2022-10-27 01:30:22 +03:00
page.getByRole("button", new Page.GetByRoleOptions().setName(Pattern.compile("submit", Pattern.CASE_INSENSITIVE))).click();
2021-12-08 20:54:01 +03:00
2022-10-04 03:02:46 +03:00
page.getByRole("checkbox", new Page.GetByRoleOptions().setChecked(true).setName("Check me"))).check();
2022-09-30 21:14:13 +03:00
```
```csharp
2022-10-27 01:30:22 +03:00
await page.GetByRole("button", new() { Name = new Regex("submit", RegexOptions.IgnoreCase) }).ClickAsync();
2021-12-08 20:54:01 +03:00
2022-10-04 03:02:46 +03:00
await page.GetByRole("checkbox", new() { Checked = true, Name = "Check me" }).CheckAsync();
2022-09-30 21:14:13 +03:00
```
2022-10-27 01:30:22 +03:00
Role locators follow W3C specificaitons for [ARIA role ](https://www.w3.org/TR/wai-aria-1.2/#roles ), [ARIA attributes ](https://www.w3.org/TR/wai-aria-1.2/#aria-attributes ) and [accessible name ](https://w3c.github.io/accname/#dfn-accessible-name ).
2022-09-30 21:14:13 +03:00
2022-10-27 01:30:22 +03:00
Note that role locators **do not replace** accessibility audits and conformance tests, but rather give early feedback about the ARIA guidelines.
2022-09-30 21:14:13 +03:00
2022-10-27 01:30:22 +03:00
### Define explicit contract and use [`method: Page.getByTestId`]
User-facing attributes like text or accessible name can change over time. In this case it is convenient to define explicit test ids.
2022-09-30 21:14:13 +03:00
```html
2022-10-27 01:30:22 +03:00
< button data-testid = "directions" > Itinéraire< / button >
2022-09-30 21:14:13 +03:00
```
```js
2022-10-27 01:30:22 +03:00
await page.getByTestId('directions').click();
2022-09-30 21:14:13 +03:00
```
```java
2022-10-27 01:30:22 +03:00
page.getByTestId("directions").click();
2022-09-30 21:14:13 +03:00
```
```python async
2022-10-27 01:30:22 +03:00
await page.get_by_test_id('directions').click()
2021-12-08 20:54:01 +03:00
```
```python sync
2022-10-27 01:30:22 +03:00
page.get_by_test_id('directions').click()
2022-09-30 21:14:13 +03:00
```
```csharp
2022-10-27 01:30:22 +03:00
await page.GetByTestId("directions").ClickAsync();
2022-09-30 21:14:13 +03:00
```
2022-10-27 01:30:22 +03:00
By default, [`method: Page.getByTestId`] will locate elements baed on the `data-testid` attribute, but you can configure it in your test config or calling [`method: Selectors.setTestIdAttribute`].
### Locate by label text with [`method: Page.getByLabel`]
2021-12-08 20:54:01 +03:00
2022-10-27 01:30:22 +03:00
Most form controls usually have dedicated labels that could be conveniently used to interact with the form. In this case, you can locate the control by its associated label.
2021-12-08 20:54:01 +03:00
2022-09-30 21:14:13 +03:00
For example, consider the following DOM structure.
2021-12-08 20:54:01 +03:00
2022-09-30 21:14:13 +03:00
```html
< label for = "password" > Password:< / label > < input type = "password" >
```
2022-10-27 01:30:22 +03:00
You can fill the input after locating it by the label text:
2022-09-30 21:14:13 +03:00
```js
2022-10-27 01:30:22 +03:00
await page.getByLabel('Password').fill('secret');
2021-12-08 20:54:01 +03:00
```
```java
2022-10-27 01:30:22 +03:00
page.getByLabel("Password").fill("secret");
2022-09-30 21:14:13 +03:00
```
2021-12-08 20:54:01 +03:00
2022-09-30 21:14:13 +03:00
```python async
2022-10-27 01:30:22 +03:00
await page.get_by_label("Password").fill("secret")
2022-09-30 21:14:13 +03:00
```
2021-12-08 20:54:01 +03:00
2022-09-30 21:14:13 +03:00
```python sync
2022-10-27 01:30:22 +03:00
page.get_by_label("Password").fill("secret")
2022-09-30 21:14:13 +03:00
```
2021-12-08 20:54:01 +03:00
2022-09-30 21:14:13 +03:00
```csharp
2022-10-27 01:30:22 +03:00
await page.GetByLabel("Password").FillAsync("secret");
2022-09-30 21:14:13 +03:00
```
### Locate in a subtree
2022-10-27 01:30:22 +03:00
You can chain methods that create a locator, like [`method: Page.getByText`] or [`method: Locator.getByRole`], to narrow down the search to a particular part of the page.
2022-09-30 21:14:13 +03:00
For example, consider the following DOM structure:
```html
2022-10-27 01:30:22 +03:00
< div data-testid = 'product-card' >
2022-09-30 21:14:13 +03:00
< span > Product 1< / span >
< button > Buy< / button >
< / div >
2022-10-27 01:30:22 +03:00
< div data-testid = 'product-card' >
2022-09-30 21:14:13 +03:00
< span > Product 2< / span >
< button > Buy< / button >
< / div >
```
For example, we can first find a product card that contains text "Product 2", and then click the button in this specific product card.
```js
2022-10-27 01:30:22 +03:00
const product = page.getByTestId('product-card').filter({ hasText: 'Product 2' });
2022-09-30 21:14:13 +03:00
2022-10-04 03:02:46 +03:00
await product.getByText('Buy').click();
2022-09-30 21:14:13 +03:00
```
```python async
2022-10-27 01:30:22 +03:00
product = page.get_by_test_id("product-card").filter(has_text="Product 2")
2022-09-30 21:14:13 +03:00
2022-10-04 03:02:46 +03:00
await product.getByText("Buy").click()
2022-09-30 21:14:13 +03:00
```
```python sync
2022-10-27 01:30:22 +03:00
product = page.get_by_test_id("product-card").filter(has_text="Product 2")
2022-09-30 21:14:13 +03:00
2022-10-04 03:02:46 +03:00
product.get_by_text("Buy").click()
2022-09-30 21:14:13 +03:00
```
```java
2022-10-27 01:30:22 +03:00
Locator product = page.getByTestId("product-card").filter(new Locator.FilterOptions().setHasText("Product 2"));
2022-09-30 21:14:13 +03:00
2022-10-04 03:02:46 +03:00
product.get_by_text("Buy").click();
2021-12-08 20:54:01 +03:00
```
```csharp
2022-10-27 01:30:22 +03:00
var product = page.GetByTestId("product-card").Filter(new() { HasText = "Product 2" });
2022-09-30 21:14:13 +03:00
2022-10-04 03:02:46 +03:00
await product.GetByText("Buy").clickAsync();
2022-09-30 21:14:13 +03:00
```
### Locate by CSS or XPath selector
2022-10-27 01:30:22 +03:00
Playwright supports CSS and XPath selectors, and auto-detects them if you omit `css=` or `xpath=` prefix. Use [`method: Page.locator`] for this:
2022-09-30 21:14:13 +03:00
```js
await page.locator('css=button').click();
await page.locator('xpath=//button').click();
await page.locator('button').click();
await page.locator('//button').click();
```
```java
page.locator("css=button").click();
page.locator("xpath=//button").click();
page.locator("button").click();
page.locator("//button").click();
```
```python async
2022-10-03 22:24:26 +03:00
await page.locator("css=button").click()
await page.locator("xpath=//button").click()
2022-09-30 21:14:13 +03:00
2022-10-03 22:24:26 +03:00
await page.locator("button").click()
await page.locator("//button").click()
2022-09-30 21:14:13 +03:00
```
```python sync
2022-10-03 22:24:26 +03:00
page.locator("css=button").click()
page.locator("xpath=//button").click()
2022-09-30 21:14:13 +03:00
2022-10-03 22:24:26 +03:00
page.locator("button").click()
page.locator("//button").click()
2022-09-30 21:14:13 +03:00
```
```csharp
await page.Locator('css=button').ClickAsync();
await page.Locator('xpath=//button').ClickAsync();
await page.Locator('button').ClickAsync();
await page.Locator('//button').ClickAsync();
```
2022-10-27 01:30:22 +03:00
XPath and CSS selectors can be tied to the DOM structure or implementation. These selectors can break when the DOM structure changes. Long CSS or XPath chains below are an example of a **bad practice** that leads to unstable tests:
2022-09-30 21:14:13 +03:00
```js
await page.locator('#tsf > div:nth-child(2) > div.A8SBwf > div.RNNXgb > div > div.a4bIc > input').click();
await page.locator('//*[@id="tsf"]/div[2]/div[1]/div[1]/div/div[2]/input').click();
```
```java
page.locator("#tsf > div:nth-child(2) > div.A8SBwf > div.RNNXgb > div > div.a4bIc > input").click();
page.locator("//*[@id='tsf']/div[2]/div[1]/div[1]/div/div[2]/input").click();
```
```python async
2022-10-03 22:24:26 +03:00
await page.locator("#tsf > div:nth-child(2) > div.A8SBwf > div.RNNXgb > div > div.a4bIc > input").click()
2022-09-30 21:14:13 +03:00
2022-10-03 22:24:26 +03:00
await page.locator("//*[@id="tsf"]/div[2]/div[1]/div[1]/div/div[2]/input").click()
2022-09-30 21:14:13 +03:00
```
2021-12-08 20:54:01 +03:00
2022-09-30 21:14:13 +03:00
```python sync
2022-10-03 22:24:26 +03:00
page.locator("#tsf > div:nth-child(2) > div.A8SBwf > div.RNNXgb > div > div.a4bIc > input").click()
2022-09-30 21:14:13 +03:00
2022-10-03 22:24:26 +03:00
page.locator("//*[@id="tsf"]/div[2]/div[1]/div[1]/div/div[2]/input").click()
2022-09-30 21:14:13 +03:00
```
2021-12-08 20:54:01 +03:00
2022-09-30 21:14:13 +03:00
```csharp
await page.Locator("#tsf > div:nth-child(2) > div.A8SBwf > div.RNNXgb > div > div.a4bIc > input").ClickAsync();
2021-12-08 20:54:01 +03:00
2022-09-30 21:14:13 +03:00
await page.Locator("//*[@id='tsf']/div[2]/div[1]/div[1]/div/div[2]/input").ClickAsync();
2021-12-08 20:54:01 +03:00
```
2022-10-27 01:30:22 +03:00
Instead, try to come up with a locator that is close to how user perceives the page or [define an explicit testing contract ](#define-explicit-contract-and-use-pagegetbytestidtestid ).
2022-04-25 22:06:18 +03:00
2022-09-30 21:14:13 +03:00
### Locate elements that contain other elements
2022-04-25 22:06:18 +03:00
2022-09-30 21:14:13 +03:00
#### Filter by text
Locator can be optionally filtered by text. It will search for a particular string somewhere inside the element, possibly in a descendant element, case-insensitively. You can also pass a regular expression.
2022-04-25 22:06:18 +03:00
```js
2022-10-27 01:30:22 +03:00
await page.getByTestId('product-card').filter({ hasText: 'Product 3' }).click();
await page.getByTestId('product-card').filter({ hasText: /product 3/ }).click();
2022-04-25 22:06:18 +03:00
```
```java
2022-10-27 01:30:22 +03:00
page.getByTestId("product-card").filter(new Locator.FilterOptions().setHasText("Product 3")).click();
page.getByTestId("product-card").filter(new Locator.FilterOptions().setHasText(Pattern.compile("Product 3"))).click();
2022-04-25 22:06:18 +03:00
```
```python async
2022-10-27 01:30:22 +03:00
await page.get_by_test_id("product-card").filter(has_text="Product 3").click()
await page.get_by_test_id("product-card").filter(has_text=re.compile("Product 3")).click()
2022-04-25 22:06:18 +03:00
```
```python sync
2022-10-27 01:30:22 +03:00
page.get_by_test_id("product-card").filter(has_text="Product 3").click()
page.get_by_test_id("product-card").filter(has_text=re.compile("Product 3")).click()
2022-04-25 22:06:18 +03:00
```
```csharp
2022-10-27 01:30:22 +03:00
await page.GetByTestId("product-card").Filter(new() { HasText = "Product 3" }).ClickAsync();
await page.GetByTestId("product-card").Filter(new() { HasText = new Regex("Product 3") }).ClickAsync();
2022-04-25 22:06:18 +03:00
```
2022-09-30 21:14:13 +03:00
#### Filter by another locator
Locators support an option to only select elements that have a descendant matching another locator.
2022-04-25 22:06:18 +03:00
```js
2022-10-27 01:30:22 +03:00
page.getByRole('section').filter({ has: page.getByTestId('subscribe-button') })
2022-04-25 22:06:18 +03:00
```
```java
2022-10-27 01:30:22 +03:00
page.getByRole("section").filter(new Locator.FilterOptions().setHas(page.getByTestId("subscribe-button")))
2022-04-25 22:06:18 +03:00
```
```python async
2022-10-27 01:30:22 +03:00
page.get_by_role("section"), has=page.get_by_test_id("subscribe-button"))
2022-04-25 22:06:18 +03:00
```
```python sync
2022-10-27 01:30:22 +03:00
page.get_by_role("section"), has=page.get_by_test_id("subscribe-button"))
2022-04-25 22:06:18 +03:00
```
```csharp
2022-10-27 01:30:22 +03:00
page.GetByRole("section"), new() { Has = page.GetByTestId("subscribe-button") })
2022-04-25 22:06:18 +03:00
```
2022-09-30 21:14:13 +03:00
Note that inner locator is matched starting from the outer one, not from the document root.
#### Augment an existing locator
You can filter an existing locator by text or another one, using [`method: Locator.filter`] method, possibly chaining it multiple times.
2022-04-25 22:06:18 +03:00
```js
2022-06-16 20:05:30 +03:00
const rowLocator = page.locator('tr');
2022-04-25 22:06:18 +03:00
// ...
2022-06-16 20:05:30 +03:00
await rowLocator
.filter({ hasText: 'text in column 1' })
2022-10-27 01:30:22 +03:00
.filter({ has: page.getByRole('button', { name: 'column 2 button' }) })
2022-06-16 20:05:30 +03:00
.screenshot();
2022-04-25 22:06:18 +03:00
```
```java
2022-06-16 20:05:30 +03:00
Locator rowLocator = page.locator("tr");
2022-04-25 22:06:18 +03:00
// ...
2022-06-16 20:05:30 +03:00
rowLocator
.filter(new Locator.FilterOptions().setHasText("text in column 1"))
.filter(new Locator.FilterOptions().setHas(
2022-10-27 01:30:22 +03:00
page.getByRole("button", new Page.GetByRoleOptions().setName("column 2 button"))
2022-06-16 20:05:30 +03:00
))
.screenshot();
2022-04-25 22:06:18 +03:00
```
```python async
2022-08-21 14:57:53 +03:00
row_locator = page.locator("tr")
2022-04-25 22:06:18 +03:00
# ...
2022-06-16 20:05:30 +03:00
await row_locator
.filter(has_text="text in column 1")
2022-10-27 01:30:22 +03:00
.filter(has=page.get_by_role("button", name="column 2 button"))
2022-06-16 20:05:30 +03:00
.screenshot()
2022-04-25 22:06:18 +03:00
```
```python sync
2022-08-21 14:57:53 +03:00
row_locator = page.locator("tr")
2022-04-25 22:06:18 +03:00
# ...
2022-06-16 20:05:30 +03:00
row_locator
.filter(has_text="text in column 1")
2022-10-27 01:30:22 +03:00
.filter(has=page.get_by_role("button", name="column 2 button"))
2022-06-16 20:05:30 +03:00
.screenshot()
2022-04-25 22:06:18 +03:00
```
```csharp
2022-06-16 20:05:30 +03:00
var rowLocator = page.Locator("tr");
2022-04-25 22:06:18 +03:00
// ...
2022-06-16 20:05:30 +03:00
await rowLocator
.Filter(new LocatorFilterOptions { HasText = "text in column 1" })
.Filter(new LocatorFilterOptions {
2022-10-27 01:30:22 +03:00
Has = page.GetByRole("button", new() { Name = "column 2 button" } )
2022-06-16 20:05:30 +03:00
})
.ScreenshotAsync();
2022-04-25 22:06:18 +03:00
```
2022-09-30 21:14:13 +03:00
### Locate elements in Shadow DOM
All locators in Playwright **by default** work with elements in Shadow DOM. The exceptions are:
2022-10-27 01:30:22 +03:00
- Locating by XPath does not pierce shadow roots.
2022-09-30 21:14:13 +03:00
- [Closed-mode shadow roots ](https://developer.mozilla.org/en-US/docs/Web/API/Element/attachShadow#parameters ) are not supported.
Consider the following example with a custom web component:
```html
2022-10-27 01:30:22 +03:00
< x-details role = button aria-expanded = true aria-controls = inner-details >
< div > Title< / div >
2022-09-30 21:14:13 +03:00
#shadow -root
2022-10-27 01:30:22 +03:00
< div id = inner-details > Details< / div >
< / x-details >
2022-09-30 21:14:13 +03:00
```
You can locate in the same way as if the shadow root was not present at all.
2022-10-27 01:30:22 +03:00
- Click `<div>Details</div>`
2022-09-30 21:14:13 +03:00
```js
2022-10-04 03:02:46 +03:00
await page.getByText('Details').click();
2022-09-30 21:14:13 +03:00
```
```java
2022-10-04 03:02:46 +03:00
page.getByText("Details").click();
2022-09-30 21:14:13 +03:00
```
```python async
2022-10-04 03:02:46 +03:00
await page.get_by_text("Details").click()
2022-09-30 21:14:13 +03:00
```
```python sync
2022-10-04 03:02:46 +03:00
page.get_by_text("Details").click()
2022-09-30 21:14:13 +03:00
```
```csharp
2022-10-04 03:02:46 +03:00
await page.GetByText("Details").ClickAsync();
2022-09-30 21:14:13 +03:00
```
2022-10-27 01:30:22 +03:00
- Click `<x-details>`
2022-09-30 21:14:13 +03:00
```js
2022-10-27 01:30:22 +03:00
await page.locator('x-details', { hasText: 'Details' }).click();
2022-09-30 21:14:13 +03:00
```
```java
2022-10-27 01:30:22 +03:00
page.locator("x-details", new Page.LocatorOptions().setHasText("Details")).click();
2022-09-30 21:14:13 +03:00
```
```python async
2022-10-27 01:30:22 +03:00
await page.locator("x-details", has_text="Details" ).click()
2022-09-30 21:14:13 +03:00
```
```python sync
2022-10-27 01:30:22 +03:00
page.locator("x-details", has_text="Details" ).click()
2022-09-30 21:14:13 +03:00
```
```csharp
2022-10-27 01:30:22 +03:00
await page.Locator("x-details", new() { HasText = "Details" }).ClickAsync();
2022-09-30 21:14:13 +03:00
```
2022-10-27 01:30:22 +03:00
- Ensure that `<x-details>` contains text "Details"
2022-09-30 21:14:13 +03:00
```js
2022-10-27 01:30:22 +03:00
await expect(page.locator('x-details')).toContainText('Details');
2022-09-30 21:14:13 +03:00
```
```java
2022-10-27 01:30:22 +03:00
assertThat(page.locator("x-details")).containsText("Details");
2022-09-30 21:14:13 +03:00
```
```python async
2022-10-27 01:30:22 +03:00
await expect(page.locator("x-details")).to_contain_text("Details")
2022-09-30 21:14:13 +03:00
```
```python sync
2022-10-27 01:30:22 +03:00
expect(page.locator("x-details")).to_contain_text("Details")
2022-09-30 21:14:13 +03:00
```
```csharp
2022-10-27 01:30:22 +03:00
await Expect(page.Locator("x-details")).ToContainTextAsync("Details");
2022-09-30 21:14:13 +03:00
```
## Lists
You can also use locators to work with the element lists.
```js
2022-10-03 22:24:26 +03:00
// Locate elements, this locator points to a list.
2022-10-27 01:30:22 +03:00
const rows = page.getByRole('listitem');
2022-10-03 22:24:26 +03:00
// Pattern 1: use locator methods to calculate text on the whole list.
const texts = await rows.allTextContents();
// Pattern 2: do something with each element in the list.
const count = await rows.count()
for (let i = 0; i < count ; + + i )
console.log(await 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.
const texts = await rows.evaluateAll(list => list.map(element => element.textContent));
2022-09-30 21:14:13 +03:00
```
2022-10-03 22:24:26 +03:00
2022-09-30 21:14:13 +03:00
```python async
2022-10-03 22:24:26 +03:00
# Locate elements, this locator points to a list.
2022-10-27 01:30:22 +03:00
rows = page.get_by_role("listitem")
2022-10-03 22:24:26 +03:00
# Pattern 1: use locator methods to calculate text on the whole list.
texts = await rows.all_text_contents()
# Pattern 2: do something with each element in the list.
count = await rows.count()
for i in range(count):
print(await rows.nth(i).text_content())
# 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 = await rows.evaluate_all("list => list.map(element => element.textContent)")
2022-09-30 21:14:13 +03:00
```
2022-10-03 22:24:26 +03:00
2022-09-30 21:14:13 +03:00
```python sync
2022-10-03 22:24:26 +03:00
# Locate elements, this locator points to a list.
2022-10-27 01:30:22 +03:00
rows = page.get_by_role("listitem")
2022-10-03 22:24:26 +03:00
# 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):
print(rows.nth(i).text_content())
# 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 => list.map(element => element.textContent)")
2022-09-30 21:14:13 +03:00
```
2022-10-03 22:24:26 +03:00
2022-09-30 21:14:13 +03:00
```java
2022-10-03 22:24:26 +03:00
// Locate elements, this locator points to a list.
2022-10-27 01:30:22 +03:00
Locator rows = page.getByRole("listitem");
2022-10-03 22:24:26 +03:00
// 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)");
2022-09-30 21:14:13 +03:00
```
2022-10-03 22:24:26 +03:00
2022-09-30 21:14:13 +03:00
```csharp
2022-10-03 22:24:26 +03:00
// Locate elements, this locator points to a list.
2022-10-27 01:30:22 +03:00
var rows = page.GetByRole("listitem");
2022-09-30 21:14:13 +03:00
2022-10-03 22:24:26 +03:00
// Pattern 1: use locator methods to calculate text on the whole list.
var texts = await rows.AllTextContentsAsync();
2022-09-30 21:14:13 +03:00
2022-10-03 22:24:26 +03:00
// 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());
2022-09-30 21:14:13 +03:00
2022-10-03 22:24:26 +03:00
// 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)");
```
2022-09-30 21:14:13 +03:00
### Picking specific element from a list
If you have a list of identical elements, and the only way to distinguish between them is the order, you can choose a specific element from a list with [`method: Locator.first`], [`method: Locator.last`] or [`method: Locator.nth`].
For example, to click the third item in the list of products:
```js
2022-10-27 01:30:22 +03:00
await page.getByTestId('product-card').nth(3).click();
2021-12-08 20:54:01 +03:00
```
```java
2022-10-27 01:30:22 +03:00
page.getByTestId("product-card").nth(3).click();
2021-12-08 20:54:01 +03:00
```
```python async
2022-10-27 01:30:22 +03:00
await page.get_by_test_id("product-card").nth(3).click()
2021-12-08 20:54:01 +03:00
```
```python sync
2022-10-27 01:30:22 +03:00
page.get_by_test_id("product-card").nth(3).click()
2021-12-08 20:54:01 +03:00
```
```csharp
2022-10-27 01:30:22 +03:00
await page.GetByTestId("product-card").Nth(3).ClickAsync();
2021-12-08 20:54:01 +03:00
```
2022-10-27 01:30:22 +03:00
However, use these methods with caution. Often times, the page might change, and locator will point to a completely different element from the one you expected. Instead, try to come up with a unique locator that will pass the [strictness criteria ](#strictness ).