docs: update locators doc to use new APIs (#18352)

This commit is contained in:
Dmitry Gozman 2022-10-26 15:30:22 -07:00 committed by GitHub
parent 6eefe205f7
commit eb1c92630e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 256 additions and 225 deletions

View File

@ -9,22 +9,22 @@ await locator.click();
```
```java
Locator locator = page.frameLocator("#my-frame").locator("text=Submit");
Locator locator = page.frameLocator("#my-frame").getByText("Submit");
locator.click();
```
```python async
locator = page.frame_locator("#my-frame").locator("text=Submit")
locator = page.frame_locator("#my-frame").get_by_text("Submit")
await locator.click()
```
```python sync
locator = page.frame_locator("my-frame").locator("text=Submit")
locator = page.frame_locator("my-frame").get_by_text("Submit")
locator.click()
```
```csharp
var locator = page.FrameLocator("#my-frame").Locator("text=Submit");
var locator = page.FrameLocator("#my-frame").GetByText("Submit");
await locator.ClickAsync();
```

View File

@ -253,3 +253,79 @@ unless page navigates or the handle is manually disposed via the [`method: JSHan
- [`method: Page.evaluateHandle`]
- [`method: Page.querySelector`]
- [`method: Page.querySelectorAll`]
## 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.
```js
const handle = await page.$('text=Submit');
// ...
await handle.hover();
await handle.click();
```
```java
ElementHandle handle = page.querySelector("text=Submit");
handle.hover();
handle.click();
```
```python async
handle = await page.query_selector("text=Submit")
await handle.hover()
await handle.click()
```
```python sync
handle = page.query_selector("text=Submit")
handle.hover()
handle.click()
```
```csharp
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.
```js
const locator = page.getByText('Submit');
// ...
await locator.hover();
await locator.click();
```
```java
Locator locator = page.getByText("Submit");
locator.hover();
locator.click();
```
```python async
locator = page.get_by_text("Submit")
await locator.hover()
await locator.click()
```
```python sync
locator = page.get_by_text("Submit")
locator.hover()
locator.click()
```
```csharp
var locator = page.GetByText("Submit");
await locator.HoverAsync();
await locator.ClickAsync();
```

View File

@ -4,7 +4,7 @@ title: "Locators"
---
[Locator]s 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 [`method: Page.locator`] method.
a way to find element(s) on the page at any moment.
```js
const locator = page.getByText('Submit');
@ -71,75 +71,57 @@ await locator.ClickAsync();
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.
given selector. For example, the following call throws if there are several buttons in the DOM:
```js
// Throws if there are several buttons in DOM:
await page.getByRole('button').click();
```
// Works because we explicitly tell locator to pick the first element:
await page.getByRole('button').first().click(); // ⚠️ using first disables strictness
```python async
await page.get_by_role("button").click()
```
// Works because count knows what to do with multiple matches:
```python sync
page.get_by_role("button").click()
```
```java
page.getByRole("button").click();
```
```csharp
await page.GetByRole("button").ClickAsync();
```
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();
```
```python async
# Throws if there are several buttons in DOM:
await page.get_by_role("button").click()
# Works because we explicitly tell locator to pick the first element:
await page.get_by_role("button").first.click() # ⚠️ using first disables strictness
# Works because count knows what to do with multiple matches:
await page.get_by_role("button").count()
```
```python sync
# Throws if there are several buttons in DOM:
page.get_by_role("button").click()
# Works because we explicitly tell locator to pick the first element:
page.get_by_role("button").first.click() # ⚠️ using first disables strictness
# Works because count knows what to do with multiple matches:
page.get_by_role("button").count()
```
```java
// Throws if there are several buttons in DOM:
page.getByRole("button").click();
// Works because we explicitly tell locator to pick the first element:
page.getByRole("button").first().click(); // ⚠️ using first disables strictness
// Works because count knows what to do with multiple matches:
page.getByRole("button").count();
```
```csharp
// Throws if there are several buttons in DOM:
await page.GetByRole("button").ClickAsync();
// Works because we explicitly tell locator to pick the first element:
await page.GetByRole("button").First.ClickAsync(); // ⚠️ using First disables strictness
// Works because Count knows what to do with multiple matches:
await page.GetByRole("button").CountAsync();
```
:::caution
Using [`method: Locator.first`], [`method: Locator.last`], and [`method: Locator.nth`] 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.
:::
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.
## Locating elements
Use [`method: Page.locator`] method to create a locator. This method takes a selector that describes how to find an element in the page. The choice of selectors determines the resiliency of the test when the underlying web page changes. To reduce the maintenance burden, we recommend prioritizing user-facing attributes and explicit contracts.
### Locate by text content using `text=`
The easiest way to find an element is to look for the text it contains.
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();
@ -157,29 +139,87 @@ page.get_by_text("Log in").click()
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();
```
### 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.
```js
await page.getByText('Log in').click();
await page.getByText('Log in', { exact: true }).click();
await page.getByText(/log in$/i).click();
```
```java
page.getByText("Log in").click();
page.getByText("Log in", new Page.GetByTextOptions().setExact(true)).click();
page.getByText(Pattern.compile("log in$", Pattern.CASE_INSENSITIVE)).click();
```
```python async
await page.get_by_text("Log in").click()
await page.get_by_text("Log in", exact=True).click()
await page.get_by_text(re.compile("Log in", re.IGNORECASE)).click()
```
```python sync
page.get_by_text("Log in").click()
page.get_by_text("Log in", exact=True).click()
page.get_by_text(re.compile("Log in", re.IGNORECASE)).click()
```
```csharp
await page.GetByText("Log in").ClickAsync();
await page.GetByText("Log in", new() { Exact: true }).ClickAsync();
await page.GetByText(new Regex("Log in", RegexOptions.IgnoreCase)).ClickAsync();
```
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
await page.locator('data-test-id=product-item', { hasText: 'Playwright Book' }).click();
await page.getByTestId('product-item').filter({ hasText: 'Playwright Book' }).click();
```
```java
page.locator("data-test-id=product-item", new Page.LocatorOptions().setHasText("Playwright Book")).click();
page.getByTestId("product-item").filter(new Locator.FilterOptions().setHasText("Playwright Book")).click();
```
```python async
await page.locator("data-test-id=product-item", has_text="Playwright Book").click()
await page.get_by_test_id("product-item").filter(has_text="Playwright Book").click()
```
```python sync
page.locator("data-test-id=product-item", has_text="Playwright Book").click()
page.get_by_test_id("product-item").filter(has_text="Playwright Book").click()
```
```csharp
await page.Locator("data-test-id=product-item", new() { HasText = "Playwright Book" }).ClickAsync();
await page.GetByTestId("product-item").Filter(new() { HasText = "Playwright Book" }).ClickAsync();
```
[Learn more about the `text` selector](./selectors.md#text-selector).
### Locate based on accessible attributes with [`method: Page.getByRole`]
### Locate based on accessible attributes using `role=`
The `role` selector 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.
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.
```js
await page.getByRole('button', { name: /submit/i }).click();
@ -188,62 +228,66 @@ await page.getByRole('checkbox', { checked: true, name: "Check me" }).check();
```
```python async
await page.get_by_role("button", name=re.compile("(?i)submit")).click()
await page.get_by_role("button", name=re.compile("submit", re.IGNORECASE)).click()
await page.get_by_role("checkbox", checked=True, name="Check me"]).check()
```
```python sync
page.get_by_role("button", name=re.compile("(?i)submit")).click()
page.get_by_role("button", name=re.compile("submit", re.IGNORECASE)).click()
page.get_by_role("checkbox", checked=True, name="Check me"]).check()
```
```java
page.getByRole("button", new Page.GetByRoleOptions().setName(Pattern.compile("(?i)submit"))).click();
page.getByRole("button", new Page.GetByRoleOptions().setName(Pattern.compile("submit", Pattern.CASE_INSENSITIVE))).click();
page.getByRole("checkbox", new Page.GetByRoleOptions().setChecked(true).setName("Check me"))).check();
```
```csharp
await page.GetByRole("button", new() { Name = new Regex("(?i)submit") }).ClickAsync();
await page.GetByRole("button", new() { Name = new Regex("submit", RegexOptions.IgnoreCase) }).ClickAsync();
await page.GetByRole("checkbox", new() { Checked = true, Name = "Check me" }).CheckAsync();
```
[Learn more about the `role` selector](./selectors.md#role-selector).
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).
### Define explicit contract and use `data-test-id=`
Note that role locators **do not replace** accessibility audits and conformance tests, but rather give early feedback about the ARIA guidelines.
User-facing attributes like text or accessible name can change frequently. In this case it is convenient to define explicit test ids, for example with a `data-test-id` attribute. Playwright has dedicated support for `id`, `data-test-id`, `data-test` and `data-testid` attributes.
### 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.
```html
<button data-test-id="directions">Itinéraire</button>
<button data-testid="directions">Itinéraire</button>
```
```js
await page.locator('data-test-id=directions').click();
await page.getByTestId('directions').click();
```
```java
page.locator("data-test-id=directions").click();
page.getByTestId("directions").click();
```
```python async
await page.locator('data-test-id=directions').click()
await page.get_by_test_id('directions').click()
```
```python sync
page.locator('data-test-id=directions').click()
page.get_by_test_id('directions').click()
```
```csharp
await page.Locator("data-test-id=directions").ClickAsync();
await page.GetByTestId("directions").ClickAsync();
```
### Locate by label text
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`].
Most form controls usually have dedicated labels that could be conveniently used to interact with the form. Input actions in Playwright automatically distinguish between labels and controls, so you can just locate the label to perform an action on the associated control.
### Locate by label text with [`method: Page.getByLabel`]
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.
For example, consider the following DOM structure.
@ -251,50 +295,40 @@ For example, consider the following DOM structure.
<label for="password">Password:</label><input type="password">
```
You can target the label with something like `text=Password` and perform the following actions on the password input:
- `click` will click the label and automatically focus the input field;
- `fill` will fill the input field;
- `inputValue` will return the value of the input field;
- `selectText` will select text in the input field;
- `setInputFiles` will set files for the input field with `type=file`;
- `selectOption` will select an option from the select box.
For example, to fill the input by targeting the label:
You can fill the input after locating it by the label text:
```js
await page.getByText('Password').fill('secret');
await page.getByLabel('Password').fill('secret');
```
```java
page.getByText("Password").fill("secret");
page.getByLabel("Password").fill("secret");
```
```python async
await page.get_by_text('Password').fill('secret')
await page.get_by_label("Password").fill("secret")
```
```python sync
page.get_by_text('Password').fill('secret')
page.get_by_label("Password").fill("secret")
```
```csharp
await page.GetByText("Password").FillAsync("secret");
await page.GetByLabel("Password").FillAsync("secret");
```
However, other methods will target the label itself, for example `textContent` will return the text content of the label, not the input field.
### Locate in a subtree
You can chain [`method: Page.locator`] and [`method: Locator.locator`] calls to narrow down the search to a particular part of the page.
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.
For example, consider the following DOM structure:
```html
<div data-test-id='product-card'>
<div data-testid='product-card'>
<span>Product 1</span>
<button>Buy</button>
</div>
<div data-test-id='product-card'>
<div data-testid='product-card'>
<span>Product 2</span>
<button>Buy</button>
</div>
@ -303,38 +337,38 @@ For example, consider the following DOM structure:
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
const product = page.locator('data-test-id=product-card', { hasText: 'Product 2' });
const product = page.getByTestId('product-card').filter({ hasText: 'Product 2' });
await product.getByText('Buy').click();
```
```python async
product = page.locator("data-test-id=product-card", has_text="Product 2")
product = page.get_by_test_id("product-card").filter(has_text="Product 2")
await product.getByText("Buy").click()
```
```python sync
product = page.locator("data-test-id=product-card", has_text="Product 2")
product = page.get_by_test_id("product-card").filter(has_text="Product 2")
product.get_by_text("Buy").click()
```
```java
Locator product = page.locator("data-test-id=product-card", new Page.LocatorOptions().setHasText("Product 2"));
Locator product = page.getByTestId("product-card").filter(new Locator.FilterOptions().setHasText("Product 2"));
product.get_by_text("Buy").click();
```
```csharp
var product = page.Locator("data-test-id=product-card", new() { HasText = "Product 2" });
var product = page.GetByTestId("product-card").Filter(new() { HasText = "Product 2" });
await product.GetByText("Buy").clickAsync();
```
### Locate by CSS or XPath selector
Playwright supports CSS and XPath selectors, and auto-detects them if you omit `css=` or `xpath=` prefix:
Playwright supports CSS and XPath selectors, and auto-detects them if you omit `css=` or `xpath=` prefix. Use [`method: Page.locator`] for this:
```js
await page.locator('css=button').click();
@ -376,11 +410,7 @@ await page.Locator('button').ClickAsync();
await page.Locator('//button').ClickAsync();
```
### Avoid locators tied to implementation
XPath and CSS selectors can be tied to the DOM structure or implementation. These selectors can break when the DOM structure changes. Similarly, [`method: Locator.nth`], [`method: Locator.first`], and [`method: Locator.last`] are tied to implementation and the structure of the DOM, and will target the incorrect element if the DOM changes.
Long CSS or XPath chains below are an example of a **bad practice** that leads to unstable tests:
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:
```js
await page.locator('#tsf > div:nth-child(2) > div.A8SBwf > div.RNNXgb > div > div.a4bIc > input').click();
@ -412,7 +442,7 @@ await page.Locator("#tsf > div:nth-child(2) > div.A8SBwf > div.RNNXgb > div > di
await page.Locator("//*[@id='tsf']/div[2]/div[1]/div[1]/div/div[2]/input").ClickAsync();
```
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-data-test-id).
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).
### Locate elements that contain other elements
@ -421,24 +451,24 @@ Instead, try to come up with a locator that is close to how user perceives the p
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.
```js
await page.locator('button', { hasText: 'Click me' }).click();
await page.locator('button', { hasText: /Click me/ }).click();
await page.getByTestId('product-card').filter({ hasText: 'Product 3' }).click();
await page.getByTestId('product-card').filter({ hasText: /product 3/ }).click();
```
```java
page.locator("button", new Page.LocatorOptions().setHasText("Click me")).click();
page.locator("button", new Page.LocatorOptions().setHasText(Pattern.compile("Click me"))).click();
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();
```
```python async
await page.locator("button", has_text="Click me").click()
await page.locator("button", has_text=re.compile("Click me")).click()
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()
```
```python sync
page.locator("button", has_text="Click me").click()
page.locator("button", has_text=re.compile("Click me")).click()
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()
```
```csharp
await page.Locator("button", new() { HasText = "Click me" }).ClickAsync();
await page.Locator("button", new() { HasText = new Regex("Click me") }).ClickAsync();
await page.GetByTestId("product-card").Filter(new() { HasText = "Product 3" }).ClickAsync();
await page.GetByTestId("product-card").Filter(new() { HasText = new Regex("Product 3") }).ClickAsync();
```
#### Filter by another locator
@ -446,19 +476,19 @@ await page.Locator("button", new() { HasText = new Regex("Click me") }).ClickAsy
Locators support an option to only select elements that have a descendant matching another locator.
```js
page.locator('article', { has: page.locator('button.subscribe') })
page.getByRole('section').filter({ has: page.getByTestId('subscribe-button') })
```
```java
page.locator("article", new Page.LocatorOptions().setHas(page.locator("button.subscribe")))
page.getByRole("section").filter(new Locator.FilterOptions().setHas(page.getByTestId("subscribe-button")))
```
```python async
page.locator("article", has=page.locator("button.subscribe"))
page.get_by_role("section"), has=page.get_by_test_id("subscribe-button"))
```
```python sync
page.locator("article", has=page.locator("button.subscribe"))
page.get_by_role("section"), has=page.get_by_test_id("subscribe-button"))
```
```csharp
page.Locator("article", new() { Has = page.Locator("button.subscribe") })
page.GetByRole("section"), new() { Has = page.GetByTestId("subscribe-button") })
```
Note that inner locator is matched starting from the outer one, not from the document root.
@ -472,7 +502,7 @@ const rowLocator = page.locator('tr');
// ...
await rowLocator
.filter({ hasText: 'text in column 1' })
.filter({ has: page.locator('button', { hasText: 'column 2 button' }) })
.filter({ has: page.getByRole('button', { name: 'column 2 button' }) })
.screenshot();
```
```java
@ -481,7 +511,7 @@ 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"))
page.getByRole("button", new Page.GetByRoleOptions().setName("column 2 button"))
))
.screenshot();
```
@ -490,7 +520,7 @@ row_locator = page.locator("tr")
# ...
await row_locator
.filter(has_text="text in column 1")
.filter(has=page.locator("tr", has_text="column 2 button"))
.filter(has=page.get_by_role("button", name="column 2 button"))
.screenshot()
```
```python sync
@ -498,7 +528,7 @@ row_locator = page.locator("tr")
# ...
row_locator
.filter(has_text="text in column 1")
.filter(has=page.locator("tr", has_text="column 2 button"))
.filter(has=page.get_by_role("button", name="column 2 button"))
.screenshot()
```
```csharp
@ -507,7 +537,7 @@ var rowLocator = page.Locator("tr");
await rowLocator
.Filter(new LocatorFilterOptions { HasText = "text in column 1" })
.Filter(new LocatorFilterOptions {
Has = page.Locator("tr", new PageLocatorOptions { HasText = "column 2 button" } )
Has = page.GetByRole("button", new() { Name = "column 2 button" } )
})
.ScreenshotAsync();
```
@ -515,21 +545,21 @@ await rowLocator
### Locate elements in Shadow DOM
All locators in Playwright **by default** work with elements in Shadow DOM. The exceptions are:
- Locating by XPath selector does not pierce shadow roots.
- Locating by XPath does not pierce shadow roots.
- [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
<x-badge>
<span>Title</span>
<x-details role=button aria-expanded=true aria-controls=inner-details>
<div>Title</div>
#shadow-root
<span>Details</span>
</x-badge>
<div id=inner-details>Details</div>
</x-details>
```
You can locate in the same way as if the shadow root was not present at all.
- Click `<span>Details</span>`
- Click `<div>Details</div>`
```js
await page.getByText('Details').click();
```
@ -546,38 +576,38 @@ You can locate in the same way as if the shadow root was not present at all.
await page.GetByText("Details").ClickAsync();
```
- Click `<x-badge>`
- Click `<x-details>`
```js
await page.locator('x-badge', { hasText: 'Details' }).click();
await page.locator('x-details', { hasText: 'Details' }).click();
```
```java
page.locator("x-badge", new Page.LocatorOptions().setHasText("Details")).click();
page.locator("x-details", new Page.LocatorOptions().setHasText("Details")).click();
```
```python async
await page.locator("x-badge", has_text="Details" ).click()
await page.locator("x-details", has_text="Details" ).click()
```
```python sync
page.locator("x-badge", has_text="Details" ).click()
page.locator("x-details", has_text="Details" ).click()
```
```csharp
await page.Locator("x-badge", new() { HasText = "Details" }).ClickAsync();
await page.Locator("x-details", new() { HasText = "Details" }).ClickAsync();
```
- Ensure that `<x-badge>` contains text "Details"
- Ensure that `<x-details>` contains text "Details"
```js
await expect(page.locator('x-badge')).toContainText('Details');
await expect(page.locator('x-details')).toContainText('Details');
```
```java
assertThat(page.locator("x-badge")).containsText("Details");
assertThat(page.locator("x-details")).containsText("Details");
```
```python async
await expect(page.locator("x-badge")).to_contain_text("Details")
await expect(page.locator("x-details")).to_contain_text("Details")
```
```python sync
expect(page.locator("x-badge")).to_contain_text("Details")
expect(page.locator("x-details")).to_contain_text("Details")
```
```csharp
await Expect(page.Locator("x-badge")).ToContainTextAsync("Details");
await Expect(page.Locator("x-details")).ToContainTextAsync("Details");
```
## Lists
@ -586,7 +616,7 @@ You can also use locators to work with the element lists.
```js
// Locate elements, this locator points to a list.
const rows = page.locator('table tr');
const rows = page.getByRole('listitem');
// Pattern 1: use locator methods to calculate text on the whole list.
const texts = await rows.allTextContents();
@ -603,7 +633,7 @@ const texts = await rows.evaluateAll(list => list.map(element => element.textCon
```python async
# Locate elements, this locator points to a list.
rows = page.locator("table tr")
rows = page.get_by_role("listitem")
# Pattern 1: use locator methods to calculate text on the whole list.
texts = await rows.all_text_contents()
@ -620,7 +650,7 @@ texts = await rows.evaluate_all("list => list.map(element => element.textContent
```python sync
# Locate elements, this locator points to a list.
rows = page.locator("table tr")
rows = page.get_by_role("listitem")
# Pattern 1: use locator methods to calculate text on the whole list.
texts = rows.all_text_contents()
@ -637,7 +667,7 @@ texts = rows.evaluate_all("list => list.map(element => element.textContent)")
```java
// Locate elements, this locator points to a list.
Locator rows = page.locator("table tr");
Locator rows = page.getByRole("listitem");
// Pattern 1: use locator methods to calculate text on the whole list.
List<String> texts = rows.allTextContents();
@ -654,7 +684,7 @@ Object texts = rows.evaluateAll("list => list.map(element => element.textContent
```csharp
// Locate elements, this locator points to a list.
var rows = page.Locator("table tr");
var rows = page.GetByRole("listitem");
// Pattern 1: use locator methods to calculate text on the whole list.
var texts = await rows.AllTextContentsAsync();
@ -673,101 +703,26 @@ var texts = await rows.EvaluateAllAsync("list => list.map(element => element.tex
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`].
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).
For example, to click the third item in the list of products:
```js
await page.locator('data-test-id=product-card').nth(3).click();
await page.getByTestId('product-card').nth(3).click();
```
```java
page.locator("data-test-id=product-card").nth(3).click();
page.getByTestId("product-card").nth(3).click();
```
```python async
await page.locator("data-test-id=product-card").nth(3).click()
await page.get_by_test_id("product-card").nth(3).click()
```
```python sync
page.locator("data-test-id=product-card").nth(3).click()
page.get_by_test_id("product-card").nth(3).click()
```
```csharp
await page.Locator("data-test-id=product-card").Nth(3).ClickAsync();
await page.GetByTestId("product-card").Nth(3).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.
```js
const handle = await page.$('text=Submit');
// ...
await handle.hover();
await handle.click();
```
```java
ElementHandle handle = page.querySelector("text=Submit");
handle.hover();
handle.click();
```
```python async
handle = await page.query_selector("text=Submit")
await handle.hover()
await handle.click()
```
```python sync
handle = page.query_selector("text=Submit")
handle.hover()
handle.click()
```
```csharp
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.
```js
const locator = page.getByText('Submit');
// ...
await locator.hover();
await locator.click();
```
```java
Locator locator = page.getByText("Submit");
locator.hover();
locator.click();
```
```python async
locator = page.get_by_text("Submit")
await locator.hover()
await locator.click()
```
```python sync
locator = page.get_by_text("Submit")
locator.hover()
locator.click()
```
```csharp
var locator = page.GetByText("Submit");
await locator.HoverAsync();
await locator.ClickAsync();
```
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).