diff --git a/docs/src/api/class-framelocator.md b/docs/src/api/class-framelocator.md index cdf0ade996..13daae5f94 100644 --- a/docs/src/api/class-framelocator.md +++ b/docs/src/api/class-framelocator.md @@ -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(); ``` diff --git a/docs/src/handles.md b/docs/src/handles.md index e2c1c75917..5b42e33b5c 100644 --- a/docs/src/handles.md +++ b/docs/src/handles.md @@ -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(); +``` diff --git a/docs/src/locators.md b/docs/src/locators.md index 5eca419fb7..6a15cbbac0 100644 --- a/docs/src/locators.md +++ b/docs/src/locators.md @@ -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 - + ``` ```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. ``` -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 -