docs: cherry-pick from ToT (#18521)

List of cherry-picked commits:
- 37cd573652
- 8e9540b7c1
- ce7fc1b9f3
- c4404ea98f
- eb1c92630e
- d4bab139b2
- 2efa96a882
- f6e642e1fa
This commit is contained in:
Max Schmitt 2022-11-02 16:15:54 -07:00 committed by GitHub
parent 63d6a35ce5
commit f95d87228a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
13 changed files with 747 additions and 368 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

@ -1074,7 +1074,7 @@ Text to locate the element for.
* since: v1.27
- `exact` <[boolean]>
Whether to find an exact match: case-sensitive and whole-string. Default to false. Ignored when locating by a regular expression.
Whether to find an exact match: case-sensitive and whole-string. Default to false. Ignored when locating by a regular expression. Note that exact match still trims whitespace.
## locator-get-by-role-role
* since: v1.27
@ -1178,7 +1178,109 @@ Locate element by the test id. By default, the `data-testid` attribute is used a
## template-locator-get-by-text
Allows locating elements that contain given text.
Allows locating elements that contain given text. Consider the following DOM structure:
```html
<div>Hello <span>world</span></div>
<div>Hello</div>
```
You can locate by text substring, exact string, or a regular expression:
```js
// Matches <span>
page.getByText('world')
// Matches first <div>
page.getByText('Hello world')
// Matches second <div>
page.getByText('Hello', { exact: true })
// Matches both <div>s
page.getByText(/Hello/)
// Matches second <div>
page.getByText(/^hello$/i)
```
```python async
# Matches <span>
page.get_by_text("world")
# Matches first <div>
page.get_by_text("Hello world")
# Matches second <div>
page.get_by_text("Hello", exact=True)
# Matches both <div>s
page.get_by_text(re.compile("Hello"))
# Matches second <div>
page.get_by_text(re.compile("^hello$", re.IGNORECASE))
```
```python sync
# Matches <span>
page.get_by_text("world")
# Matches first <div>
page.get_by_text("Hello world")
# Matches second <div>
page.get_by_text("Hello", exact=True)
# Matches both <div>s
page.get_by_text(re.compile("Hello"))
# Matches second <div>
page.get_by_text(re.compile("^hello$", re.IGNORECASE))
```
```java
// Matches <span>
page.getByText("world")
// Matches first <div>
page.getByText("Hello world")
// Matches second <div>
page.getByText("Hello", new Page.GetByTextOptions().setExact(true))
// Matches both <div>s
page.getByText(Pattern.compile("Hello"))
// Matches second <div>
page.getByText(Pattern.compile("^hello$", Pattern.CASE_INSENSITIVE))
```
```csharp
// Matches <span>
page.GetByText("world")
// Matches first <div>
page.GetByText("Hello world")
// Matches second <div>
page.GetByText("Hello", new() { Exact: true })
// Matches both <div>s
page.GetByText(new Regex("Hello"))
// Matches second <div>
page.GetByText(new Regex("^hello$", RegexOptions.IgnoreCase))
```
See also [`method: Locator.filter`] that allows to match by another criteria, like an accessible role, and then filter by the text content.
:::note
Matching by text always normalizes whitespace, even with exact match. For example, it turns multiple spaces into one, turns line breaks into spaces and ignores leading and trailing whitespace.
:::
:::note
Input elements of the type `button` and `submit` are matched by their `value` instead of the text content. For example, locating by text `"Log in"` matches `<input type=button value="Log in">`.
:::
## template-locator-get-by-alt-text

View file

@ -13,37 +13,34 @@ Playwright comes with the ability to generate tests out of the box and is a grea
## Running Codegen
```bash js
npx playwright codegen playwright.dev
npx playwright codegen demo.playwright.dev/todomvc
```
```bash java
mvn exec:java -e -Dexec.mainClass=com.microsoft.playwright.CLI -Dexec.args="codegen playwright.dev"
mvn exec:java -e -Dexec.mainClass=com.microsoft.playwright.CLI -Dexec.args="codegen demo.playwright.dev/todomvc"
```
```bash python
playwright codegen playwright.dev
playwright codegen demo.playwright.dev/todomvc
```
```bash csharp
pwsh bin/Debug/netX/playwright.ps1 codegen playwright.dev
pwsh bin/Debug/netX/playwright.ps1 codegen demo.playwright.dev/todomvc
```
Run `codegen` and perform actions in the browser. Playwright will generate the code for the user interactions. `Codegen` will attempt to generate resilient text-based selectors.
<img width="1183" alt="Codegen generating code for tests for playwright.dev website" src="https://user-images.githubusercontent.com/13063165/181852815-971c10da-0b55-4e54-8a73-77e1e825193c.png" />
When you have finished interacting with the page, press the **record** button to stop the recording and use the **copy** button to copy the generated code to your editor.
<img width="1266" alt="Codegen generating code for tests for playwright.dev" src="https://user-images.githubusercontent.com/13063165/183905981-003c4173-0d5e-4960-8190-50e6ca71b2c3.png" />
<video width="100%" height="100%" controls muted >
<source src="https://user-images.githubusercontent.com/13063165/197979804-c4fa3347-8fab-4526-a728-c1b2fbd079b4.mp4" type="video/mp4" />
Your browser does not support the video tag.
</video>
When you have finished interacting with the page, press the **record** button to stop the recording and use the **copy** button to copy the generated code to your editor.
Use the **clear** button to clear the code to start recording again. Once finished close the Playwright inspector window or stop the terminal command.
To learn more about generating tests check out or detailed guide on [Codegen](./codegen.md).
## What's Next
- [See a trace of your tests](./trace-viewer-intro.md)

View file

@ -5,28 +5,31 @@ title: "Test Generator"
Playwright comes with the ability to generate tests out of the box and is a great way to quickly get started with testing. It will open two windows, a browser window where you interact with the website you wish to test and the Playwright Inspector window where you can record your tests, copy the tests, clear your tests as well as change the language of your tests.
## Running Codegen
```bash js
npx playwright codegen playwright.dev
npx playwright codegen demo.playwright.dev/todomvc
```
```bash java
mvn exec:java -e -Dexec.mainClass=com.microsoft.playwright.CLI -Dexec.args="codegen playwright.dev"
mvn exec:java -e -Dexec.mainClass=com.microsoft.playwright.CLI -Dexec.args="codegen demo.playwright.dev/todomvc"
```
```bash python
playwright codegen playwright.dev
playwright codegen demo.playwright.dev/todomvc
```
```bash csharp
pwsh bin/Debug/netX/playwright.ps1 codegen playwright.dev
pwsh bin/Debug/netX/playwright.ps1 codegen demo.playwright.dev/todomvc
```
Run `codegen` and perform actions in the browser. Playwright will generate the code for the user interactions. `Codegen` will attempt to generate resilient text-based selectors.
<img width="1183" alt="Codegen generating code for tests for playwright.dev website" src="https://user-images.githubusercontent.com/13063165/181852815-971c10da-0b55-4e54-8a73-77e1e825193c.png" />
<video width="100%" height="100%" controls muted>
<source src="https://user-images.githubusercontent.com/13063165/197979804-c4fa3347-8fab-4526-a728-c1b2fbd079b4.mp4" type="video/mp4" />
Your browser does not support the video tag.
</video>
## Emulate viewport size
@ -72,8 +75,7 @@ playwright codegen --device="iPhone 11" playwright.dev
pwsh bin/Debug/netX/playwright.ps1 codegen --device="iPhone 11" playwright.dev
```
<img width="1239" alt="Codegen generating code for tests for playwright.dev website emulated for iPhone 11" src="https://user-images.githubusercontent.com/13063165/182360089-9dc6d33d-480e-4bb2-86a3-fec51c1c228e.png" />
<img width="1254" alt="Codegen generating code for tests for playwright.dev website emulated for iPhone 11" src="https://user-images.githubusercontent.com/13063165/197976789-ee25ed24-69af-4684-b6a4-098673cfb035.png" />
## Emulate color scheme

View file

@ -11,7 +11,7 @@ Get started by installing Playwright and generating a test to see it in action.
Install the [VS Code extension from the marketplace](https://marketplace.visualstudio.com/items?itemName=ms-playwright.playwright) or from the extensions tab in VS Code.
<img width="1099" alt="VS Code extension for Playwright" src="https://user-images.githubusercontent.com/13063165/188664251-e6e28648-25fb-45bb-98f5-ac6044938475.png" />
<img width="1100" alt="VS Code extension for Playwright" src="https://user-images.githubusercontent.com/13063165/197744119-5ed72385-2037-450b-b988-83b2f7554cf1.png" />
Once installed, open the command panel and type:
@ -19,39 +19,42 @@ Once installed, open the command panel and type:
Install Playwright
```
<img width="1093" alt="Install Playwright in VS code" src="https://user-images.githubusercontent.com/13063165/188664853-7b3b610b-70ce-4674-ac51-3f2b48dcc589.png" />
<img width="1100" alt="Install Playwright" src="https://user-images.githubusercontent.com/13063165/197744677-edd437e7-15b2-4e3a-8c6b-e728cfe7b65c.png" />
Select **Test: Install Playwright** and Choose the browsers you would like to run your tests on. These can be later configured in the [playwright.config](./test-configuration.md) file. You can also choose if you would like to have a GitHub Actions setup to [run your tests on CI](./ci-intro.md).
<img width="1093" alt="choose browsers for Playwright in VS Code" src="https://user-images.githubusercontent.com/13063165/188664742-371f2321-67a1-4799-99ba-253a125de838.png" />
<img width="1115" alt="Choose Browsers" src="https://user-images.githubusercontent.com/13063165/197704489-72744c50-81ea-4716-a5f1-52ca801edf1f.png" />
## Running Tests
You can run a single test by clicking the green triangle next to your test block to run your test. Playwright will run through each line of the test and when it finishes you will see a green tick next to your test block as well as the time it took to run the test.
<img width="1272" alt="Running Tests in VS Code" src="https://user-images.githubusercontent.com/13063165/188641041-e7f49b0e-758c-4154-b719-b873ba58dca4.png" />
<img width="1114" alt="Run a single test" src="https://user-images.githubusercontent.com/13063165/197712138-f4593c0d-ec7e-4a61-b2cd-59fc2af39c6a.png" />
### Run Tests and Show Browsers
You can also run your tests and show the browsers by selecting the option **Show Browsers** in the testing sidebar. Then when you click the green triangle to run your test the browser will open and you will visually see it run through your test. Leave this selected if you want browsers open for all your tests or uncheck it if you prefer your tests to run in headless mode with no browser open.
<img width="1394" alt="Run Tests and Show Browsers in VS Code" src="https://user-images.githubusercontent.com/13063165/188662739-5b191b2d-7055-4f33-9399-bc8626163293.png" />
<img width="1350" alt="Show browsers while running tests" src="https://user-images.githubusercontent.com/13063165/197714311-1d8c0955-9c5b-44ec-b429-160fa3d6b7a4.png" />
Use the **Close all browsers** button to close all browsers.
<img width="1272" alt="Close Browsers in VS Code" src="https://user-images.githubusercontent.com/13063165/188663381-c0293d02-75f9-46d4-852f-43aebe508d4a.png" />
### View and Run All Tests
View all tests in the testing sidebar and extend the tests by clicking on each test. Tests that have not been run will not have the green check next to them. Run all tests by clicking on the white triangle as you hover over the tests in the testing sidebar.
<img width="1272" alt="View and Run All Tests in VS Code" src="https://user-images.githubusercontent.com/13063165/188641364-3bfa74f8-2e8a-45e5-92e1-4cbee0660e8a.png" />
<img width="1114" alt="Run all tests in file" src="https://user-images.githubusercontent.com/13063165/197712455-496f5300-79ed-4eae-9cc1-52cc9f3c019b.png" />
### Run Tests on Specific Browsers
The VS Code test runner runs your tests on the default browser of Chrome. To run on other/multiple browsers click the play button's dropdown and choose the option of "Select Default Profile" and select the browsers you wish to run your tests on.
The VS Code test runner runs your tests on the default browser of Chrome. To run on other/multiple browsers click the play button's dropdown and choose another profile or modify the default profile by clicking **Select Default Profile** and select the browsers you wish to run your tests on.
<img width="1272" alt="Run Tests on Specific Browsers in VS Code" src="https://user-images.githubusercontent.com/13063165/188642000-f3c59179-8b44-40cb-a573-c2d9965737a6.png" />
<img width="1116" alt="selecting browsers" src="https://user-images.githubusercontent.com/13063165/197728519-5381efc0-30d4-490e-82a8-e43eb35daf9f.png" />
Choose various or all profiles to run tests on multiple profiles. These profiles are read from the [playwright.config](./test-configuration.md) file. To add more profiles such as a mobile profile, first add it to your config file and it will then be available here.
<img width="1012" alt="choosing default profiles" src="https://user-images.githubusercontent.com/13063165/197710323-ec752f91-86c5-45c8-81b3-eac2e8ed0bfb.png" />
## Debugging Tests
@ -61,13 +64,13 @@ With the VS Code extension you can debug your tests right in VS Code see error m
If your test fails VS Code will show you error messages right in the editor showing what was expected, what was received as well as a complete call log.
<img width="1272" alt="Error Messages in VS Code" src="https://user-images.githubusercontent.com/13063165/188642424-37da9e6c-b24a-4755-b14c-ceefa59483d2.png" />
<img width="1339" alt="error messaging in vs code" src="https://user-images.githubusercontent.com/13063165/197967016-b4c35689-0c04-4ea3-a288-35b98056efec.png" />
### Run in Debug Mode
To set a breakpoint click next to the line number where you want the breakpoint to be until a red dot appears. Run the tests in debug mode by right clicking on the line next to the test you want to run. A browser window will open and the test will run and pause at where the breakpoint is set.
<img width="1272" alt="Run tests in Debug Mode in VS Code" src="https://user-images.githubusercontent.com/13063165/188642947-48f4eeaa-486d-4657-9819-63ad742ee7e2.png" />
<img width="1149" alt="setting debug test mode" src="https://user-images.githubusercontent.com/13063165/197715919-98f32957-2ae1-478b-9588-d93cc4548c67.png" />
### Live Debugging
@ -75,37 +78,42 @@ To set a breakpoint click next to the line number where you want the breakpoint
You can modify your test right in VS Code while debugging and Playwright will highlight the selector in the browser. This is a great way of seeing if the selector exits or if there is more than one result. You can step through the tests, pause the test and rerun the tests from the menu in VS Code.
<img width="1394" alt="Live debugging in VS Code" src="https://user-images.githubusercontent.com/13063165/188644314-89967ab8-2415-4e55-bbca-b3840d347ca4.png" />
<img width="1350" alt="Live Debugging in VS Code" src="https://user-images.githubusercontent.com/13063165/197967885-512df81f-12e3-45e5-b90f-42ed0f064eac.png" />
### Debug in different Browsers
Debug your tests on specific browsers by selecting a profile from the dropdown. Set the default profile or select more than one profile to debug various profiles. Playwright will launch the first profile and once finished debugging it will then launch the next one.
<img width="1221" alt="debugging on specific profile" src="https://user-images.githubusercontent.com/13063165/197738552-06aa8a83-6a6b-4aad-ab23-d449640e1f5f.png" />
To learn more about debugging, see [Debugging in Visual Studio Code](https://code.visualstudio.com/docs/editor/debugging).
## Generating Tests
CodeGen will auto generate your tests for you as you perform actions in the browser and is a great way to quickly get started. The viewport for the browser window is set to a specific width and height. See the [configuration guide](./test-configuration.md) to change the viewport or emulate different environments.
### Record a New Test
To record a test click on the **Record new** button from the Testing sidebar. This will create a `test-1.spec.ts` file as well as open up a browser window. In the browser go to the URL you wish to test and start clicking around. Playwright will record your actions and generate a test for you. Once you are done recording click the **cancel** button or close the browser window. You can then inspect your `test-1.spec.ts` file and see your generated test.
<img width="1272" alt="Recording a Test in VS Code" src="https://user-images.githubusercontent.com/13063165/188644755-2ab9c826-79a9-4c52-8963-26bb9e853170.png" />
<video width="100%" height="100%" controls muted>
<source src="https://user-images.githubusercontent.com/13063165/197721416-e525dd60-51a6-4740-ad8b-0f56f4d20045.mp4" type="video/mp4" />
Your browser does not support the video tag.
</video>
### Record a Test Starting From Another Test
### Record From Here
Use the **Record from here** button to record a test from a specific line in your test file. This will open up a browser window and record the test from the line you selected. A new test file will now be created with the name `test-2.spec.ts` and will include the test code up to the selected line of the test file where you ran the **Record from here** button. You can then continue to generate the new test by clicking around in the browser window.
Record a new test snippet. This creates a new empty test file but the recording starts from the current browser state from the previous test instead of starting a new browser. This snippet can then be pasted into a previous test file so it can be properly run. Note in the example below the test starts from the last state of a previous test and therefore has no `page.goto()` action.
<img width="1272" alt="Record a test from here in VS Code" src="https://user-images.githubusercontent.com/13063165/188654397-dc6e8677-e957-48ca-906e-8dd38da97c3b.png" />
### Selector Highlighting
As you interact with the page Codegen will generate the test for you in the newly created file in VS Code. When you hover over an element Playwright will highlight the element and show the [selector](./selectors.md) underneath it.
<img width="1394" alt="Selector Highlighting in VS Code" src="https://user-images.githubusercontent.com/13063165/188645469-cd9e925a-fb75-4250-bbdd-f14f2338ba34.png" />
<img width="1392" alt="record a test from a specific browser state" src="https://user-images.githubusercontent.com/13063165/197740755-fa845cbb-6292-44a4-8134-af1ce15f438a.png" />
### Picking a Selector
Pick a selector and copy it into your test file by clicking the **Pick selector** button form the testing sidebar. Then in the browser click the selector you require and it will now show up in the **Pick selector** box in VS Code. Press 'enter' on your keyboard to copy the selector into the clipboard and then paste anywhere in your code. Or press 'escape' if you want to cancel.
<img width="1394" alt="Selector Highlighting in VS Code" src="https://user-images.githubusercontent.com/13063165/188645977-2d5d1a50-d0f0-4d2e-ba30-59899bd3c77c.png" />
<img width="1394" alt="Pick selectors" src="https://user-images.githubusercontent.com/13063165/197714946-cb82231d-a6f8-4183-b54b-3375ffaa7092.png" />

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,31 +4,68 @@ 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.
### Quick Guide
These are the recommended built in locators.
- [`method: Page.getByRole`] to locate by explicit and implicit accessibility attributes.
- [`method: Page.getByText`] to locate by text content.
- [`method: Page.getByLabel`] to locate a form control by associated label's text.
- [`method: Page.getByPlaceholder`] to locate an input by placeholder.
- [`method: Page.getByAltText`] to locate an element, usually image, by its text alternative.
- [`method: Page.getByTitle`] to locate an element by its title.
- [`method: Page.getByTestId`] to locate an element based on its `data-testid` attribute (other attribute can be configured).
```js
const locator = page.getByText('Submit');
await locator.click();
await page.getByLabel('User Name').fill('John');
await page.getByLabel('Password').fill('secret-password');
await page.getByRole('button', { name: 'Sign in' }).click();
await expect(page.getByText('Welcome, John!')).toBeVisible();
```
```java
Locator locator = page.getByText("Submit");
locator.click();
page.getByLabel("User Name").fill("John");
page.getByLabel("Password").fill("secret-password");
page.getByRole(AriaRole.BUTTON, new Page.GetByRoleOptions().setName("Sign in")).click();
assertThat(page.getByText("Welcome, John!")).isVisible();
```
```python async
locator = page.get_by_text("Submit")
await locator.click()
await page.get_by_label("User Name").fill("John")
await page.get_by_label("Password").fill("secret-password")
await page.get_by_role("button", name="Sign in").click()
await expect(page.get_by_text("Welcome, John!")).to_be_visible()
```
```python sync
locator = page.get_by_text("Submit")
locator.click()
page.get_by_label("User Name").fill("John")
page.get_by_label("Password").fill("secret-password")
page.get_by_role("button", name="Sign in").click()
expect(page.get_by_text("Welcome, John!")).to_be_visible()
```
```csharp
var locator = page.GetByText("Submit");
await locator.ClickAsync();
await page.GetByLabel("User Name").FillAsync("John");
await page.GetByLabel("Password").FillAsync("secret-password");
await page.GetByRole("button", new() { Name = "Sign in" }).ClickAsync();
await Expect(page.GetByText("Welcome, John!")).ToBeVisibleAsync();
```
Every time locator is used for some action, up-to-date DOM element is located in the page. So in the snippet
@ -71,75 +108,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 +176,38 @@ page.get_by_text("Log in").click()
await page.GetByText("Log in").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.
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
await page.locator('data-test-id=product-item', { hasText: 'Playwright Book' }).click();
const locator = page.frameLocator('#my-frame').getByText('Submit');
await locator.click();
```
```java
page.locator("data-test-id=product-item", new Page.LocatorOptions().setHasText("Playwright Book")).click();
Locator locator = page.frameLocator("#my-frame").getByText("Submit");
locator.click();
```
```python async
await page.locator("data-test-id=product-item", has_text="Playwright Book").click()
locator = page.frame_locator("#my-frame").get_by_text("Submit")
await locator.click()
```
```python sync
page.locator("data-test-id=product-item", has_text="Playwright Book").click()
locator = page.frame_locator("my-frame").get_by_text("Submit")
locator.click()
```
```csharp
await page.Locator("data-test-id=product-item", new() { HasText = "Playwright Book" }).ClickAsync();
var locator = page.FrameLocator("#my-frame").GetByText("Submit");
await locator.ClickAsync();
```
[Learn more about the `text` selector](./selectors.md#text-selector).
### Locate based on accessible attributes
### 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 perceive 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,113 +216,255 @@ 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()
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()
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 specifications 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=`
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.
```html
<button data-test-id="directions">Itinéraire</button>
```
```js
await page.locator('data-test-id=directions').click();
```
```java
page.locator("data-test-id=directions").click();
```
```python async
await page.locator('data-test-id=directions').click()
```
```python sync
page.locator('data-test-id=directions').click()
```
```csharp
await page.Locator("data-test-id=directions").ClickAsync();
```
Note that role locators **do not replace** accessibility audits and conformance tests, but rather give early feedback about the ARIA guidelines.
### Locate by label text
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.
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 using [`method: Page.getByLabel`].
For example, consider the following DOM structure.
```html
<label for="password">Password:</label><input type="password">
<label for="password">Password:</label><input type="password" id="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 by placeholder text
Inputs may have a placeholder attribute to hint to the user what value should be entered. You can locate such an input using [`method: Page.getByPlaceholder`].
For example, consider the following DOM structure.
```html
<input id="email" name="email" type="email" placeholder="name@example.com">
```
You can fill the input after locating it by the placeholder text:
```js
await page.getByPlaceholder("name@example.com").fill("playwright@microsoft.com");
```
```java
page.getByPlaceholder("name@example.com").fill("playwright@microsoft.com");
```
```python async
await page.get_by_placeholder("name@example.com").fill("playwright@microsoft.com")
```
```python sync
page.get_by_placeholder("name@example.com").fill("playwright@microsoft.com")
```
```csharp
await page.GetByPlacheolder("name@example.com").FillAsync("playwright@microsoft.com");
```
### Locate by text
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 when using [`method: Page.getByText`].
```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.getByTestId('product-item').filter({ hasText: 'Playwright Book' }).click();
```
```java
page.getByTestId("product-item").filter(new Locator.FilterOptions().setHasText("Playwright Book")).click();
```
```python async
await page.get_by_test_id("product-item").filter(has_text="Playwright Book").click()
```
```python sync
page.get_by_test_id("product-item").filter(has_text="Playwright Book").click()
```
```csharp
await page.GetByTestId("product-item").Filter(new() { HasText = "Playwright Book" }).ClickAsync();
```
:::note
Matching by text always normalizes whitespace, even with exact match. For example, it turns multiple spaces into one, turns line breaks into spaces and ignores leading and trailing whitespace.
:::
### Locate by alt text
All images should have an `alt` attribute that describes the image. You can locate an image based on the text alternative using [`method: Page.getByAltText`].
For example, consider the following DOM structure.
```html
<img alt="playwright logo" src="/playwright-logo.png" />
```
You can click on the image after locating it by the text alternative:
```js
await page.getByAltText('playwright logo').click();
```
```java
page.getByAltText("playwright logo").click();
```
```python async
await page.get_by_alt_text("playwright logo").click()
```
```python sync
page.get_by_alt_text("playwright logo").click()
```
```csharp
await page.GetByAltText("playwright logo").ClickAsync();
```
### Locate by title
Locate an element with a matching title attribute using [`method: Page.getByTitle`].
For example, consider the following DOM structure.
```html
<span title='Issues count'>25 issues</span>
```
You can check the issues count after locating it by the title text:
```js
await expect(page.getByTitle('Issues count')).toHaveText('25 issues');
```
```java
assertThat(page.getByTitle("Issues count")).hasText("25 issues");
```
```python async
await expect(page.get_by_title("Issues count")).to_have_text("25 issues")
```
```python sync
expect(page.get_by_title("Issues count")).to_have_text("25 issues")
```
```csharp
await Expect(page.GetByTitle("Issues count")).toHaveText("25 issues");
```
### Define explicit contract and use a data-testid attribute
User-facing attributes like text or accessible name can change over time. In this case it is convenient to define explicit test ids and query them with [`method: Page.getByTestId`].
```html
<button data-testid="directions">Itinéraire</button>
```
```js
await page.getByTestId('directions').click();
```
```java
page.getByTestId("directions").click();
```
```python async
await page.get_by_test_id("directions").click()
```
```python sync
page.get_by_test_id("directions").click()
```
```csharp
await page.GetByTestId("directions").ClickAsync();
```
By default, [`method: Page.getByTestId`] will locate elements based on the `data-testid` attribute, but you can configure it in your test config or calling [`method: Selectors.setTestIdAttribute`].
### 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 +473,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 +546,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 +578,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 +587,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 +612,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").filter(has=page.get_by_test_id("subscribe-button"))
```
```python sync
page.locator("article", has=page.locator("button.subscribe"))
page.get_by_role("section").filter(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 +638,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 +647,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 +656,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 +664,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 +673,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 +681,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 +712,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 +752,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 +769,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 +786,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 +803,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 +820,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 +839,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).

View file

@ -82,7 +82,7 @@ Since Playwright runs in Node.js, you can debug it with your debugger of choice
npx playwright test example.spec.ts:42 --debug
```
<img width="1188" alt="Debugging Tests" src="https://user-images.githubusercontent.com/13063165/181847661-7ec5fb6c-7c21-4db0-9931-a593b21bafc2.png" />
<img width="1350" alt="Debugging Tests with the Playwright inspector" src="https://user-images.githubusercontent.com/13063165/197800771-50cb2f39-2345-4153-b4ed-de9fe63ba29b.png" />
Check out our [debugging guide](./debug.md) to learn more about the [Playwright Inspector](./debug.md#playwright-inspector) as well as debugging with [Browser Developer tools](./debug.md#browser-developer-tools).

View file

@ -236,7 +236,7 @@ await page.Locator("text=Log in").ClickAsync();
Text selector has a few variations:
- `text=Log in` - default matching is case-insensitive and searches for a substring. For example, `text=Log` matches `<button>Log in</button>`.
- `text=Log in` - default matching is case-insensitive, trims whitespace and searches for a substring. For example, `text=Log` matches `<button>Log in</button>`.
```js
await page.locator('text=Log in').click();
@ -254,7 +254,7 @@ Text selector has a few variations:
await page.Locator("text=Log in").ClickAsync();
```
- `text="Log in"` - text body can be escaped with single or double quotes to search for a text node with exact content. For example, `text="Log"` does not match `<button>Log in</button>` because `<button>` contains a single text node `"Log in"` that is not equal to `"Log"`. However, `text="Log"` matches `<button>Log<span>in</span></button>`, because `<button>` contains a text node `"Log"`. This exact mode implies case-sensitive matching, so `text="Download"` will not match `<button>download</button>`.
- `text="Log in"` - text body can be escaped with single or double quotes to search for a text node with exact content after trimming whitespace. For example, `text="Log"` does not match `<button>Log in</button>` because `<button>` contains a single text node `"Log in"` that is not equal to `"Log"`. However, `text="Log"` matches `<button> Log <span>in</span></button>`, because `<button>` contains a text node `" Log "`. This exact mode implies case-sensitive matching, so `text="Download"` will not match `<button>download</button>`.
Quoted body follows the usual escaping rules, e.g. use `\"` to escape double quote in a double-quoted string: `text="foo\"bar"`.
@ -310,7 +310,7 @@ Text selector has a few variations:
await page.Locator("text=/Log\\s*in/i").ClickAsync();
```
- `article:has-text("Playwright")` - the `:has-text()` pseudo-class can be used inside a [css] selector. It matches any element containing specified text somewhere inside, possibly in a child or a descendant element. Matching is case-insensitive and searches for a substring. For example, `article:has-text("Playwright")` matches `<article><div>Playwright</div></article>`.
- `article:has-text("Playwright")` - the `:has-text()` pseudo-class can be used inside a [css] selector. It matches any element containing specified text somewhere inside, possibly in a child or a descendant element. Matching is case-insensitive, trims whitestapce and searches for a substring. For example, `article:has-text("Playwright")` matches `<article><div>Playwright</div></article>`.
Note that `:has-text()` should be used together with other `css` specifiers, otherwise it will match all the elements containing specified text, including the `<body>`.
```js

View file

@ -5,7 +5,7 @@ title: "Writing Tests"
Playwright assertions are created specifically for the dynamic web. Checks are automatically retried until the necessary conditions are met. Playwright comes with [auto-wait](./actionability.md) built in meaning it waits for elements to be actionable prior to performing actions. Playwright provides the [Expect](./test-assertions) function to write assertions.
Take a look at the example test below to see how to write a test using web first assertions, locators and selectors.
Take a look at the example test below to see how to write a test using using [locators](/locators.md) and web first assertions.
<Tabs
groupId="test-runners"
@ -36,7 +36,7 @@ public class Tests : PageTest
await Expect(Page).ToHaveTitleAsync(new Regex("Playwright"));
// create a locator
var getStarted = Page.Locator("text=Get Started");
var getStarted = Page.GetByRole(AriaRole.Link, new() { NameString = "Get started" });
// Expect an attribute "to be strictly equal" to the value.
await Expect(getStarted).ToHaveAttributeAsync("href", "/docs/intro");
@ -71,7 +71,7 @@ public class UnitTest1 : PageTest
await Expect(Page).ToHaveTitleAsync(new Regex("Playwright"));
// create a locator
var getStarted = Page.Locator("text=Get Started");
var getStarted = Page.GetByRole(AriaRole.Link, new() { NameString = "Get started" });
// Expect an attribute "to be strictly equal" to the value.
await Expect(getStarted).ToHaveAttributeAsync("href", "/docs/intro");
@ -99,22 +99,15 @@ await Expect(Page).ToHaveTitleAsync(new Regex("Playwright"));
### Locators
[Locators](./locators.md) are the central piece of Playwright's auto-waiting and retry-ability. Locators represent a way to find element(s) on the page at any moment and are used to perform actions on elements such as `.ClickAsync` `.FillAsync` etc. Custom locators can be created with the [`method: Page.locator`] method.
[Locators](./locators.md) are the central piece of Playwright's auto-waiting and retry-ability. Locators represent a way to find element(s) on the page at any moment and are used to perform actions on elements such as `.ClickAsync` `.FillAsync` etc.
```csharp
var getStarted = Page.Locator("text=Get Started");
var getStarted = Page.GetByRole(AriaRole.Link, new() { NameString = "Get started" });
await Expect(getStarted).ToHaveAttributeAsync("href", "/docs/installation");
await getStarted.ClickAsync();
```
[Selectors](./selectors.md) are strings that are used to create Locators. Playwright supports many different selectors like [Text](./selectors.md#text-selector), [CSS](./selectors.md#css-selector), [XPath](./selectors.md#xpath-selectors) and many more. Learn more about available selectors and how to pick one in this [in-depth guide](./selectors.md).
```csharp
await Expect(Page.Locator("text=Installation")).ToBeVisibleAsync();
```
### Test Isolation
The Playwright NUnit and MSTest test framework base classes will isolate each test from each other by providing a separate `Page` instance. Pages are isolated between tests due to the Browser Context, which is equivalent to a brand new browser profile, where every test gets a fresh environment, even when multiple tests run in a single Browser.

View file

@ -15,7 +15,7 @@ Playwright assertions are created specifically for the dynamic web. Checks are a
## The Example Test
Take a look at the example test included when installing Playwright to see how to write a test using [web first assertions](/test-assertions.md), [locators](/locators.md) and [selectors](/selectors.md).
Take a look at the example test included when installing Playwright to see how to write a test using [locators](/locators.md) and [web first assertions](/test-assertions.md).
```js tab=js-js
// @ts-check
@ -28,7 +28,7 @@ test('homepage has Playwright in title and get started link linking to the intro
await expect(page).toHaveTitle(/Playwright/);
// create a locator
const getStarted = page.getByText('Get Started');
const getStarted = page.getByRole('link', { name: 'Get started' });
// Expect an attribute "to be strictly equal" to the value.
await expect(getStarted).toHaveAttribute('href', '/docs/intro');
@ -51,7 +51,7 @@ test('homepage has Playwright in title and get started link linking to the intro
await expect(page).toHaveTitle(/Playwright/);
// create a locator
const getStarted = page.getByText('Get Started');
const getStarted = page.getByRole('link', { name: 'Get started' });
// Expect an attribute "to be strictly equal" to the value.
await expect(getStarted).toHaveAttribute('href', '/docs/intro');
@ -79,23 +79,15 @@ await expect(page).toHaveTitle(/Playwright/);
### Locators
[Locators](./locators.md) are the central piece of Playwright's auto-waiting and retry-ability. Locators represent a way to find element(s) on the page at any moment and are used to perform actions on elements such as `.click` `.fill` etc. Custom locators can be created with the [`method: Page.locator`] method.
[Locators](./locators.md) are the central piece of Playwright's auto-waiting and retry-ability. Locators represent a way to find element(s) on the page at any moment and are used to perform actions on elements such as `.click` `.fill` etc.
```js
const getStarted = page.getByText('Get Started');
const getStarted = page.getByRole('link', { name: 'Get started' });
await expect(getStarted).toHaveAttribute('href', '/docs/installation');
await getStarted.click();
```
[Selectors](./selectors.md) are strings that are used to create Locators. Playwright supports many different selectors like [Text](./selectors.md#text-selector), [CSS](./selectors.md#css-selector), [XPath](./selectors.md#xpath-selectors) and many more. Learn more about available selectors and how to pick one in this [in-depth guide](./selectors.md).
```js
await expect(page.getByText('Installation')).toBeVisible();
```
### Test Isolation
Playwright Test is based on the concept of [test fixtures](./test-fixtures.md) such as the [built in page fixture](./test-fixtures#built-in-fixtures), which is passed into your test. Pages are isolated between tests due to the Browser Context, which is equivalent to a brand new browser profile, where every test gets a fresh environment, even when multiple tests run in a single Browser.

View file

@ -5,7 +5,7 @@ title: "Writing Tests"
Playwright assertions are created specifically for the dynamic web. Checks are automatically retried until the necessary conditions are met. Playwright comes with [auto-wait](./actionability.md) built in meaning it waits for elements to be actionable prior to performing actions. Playwright provides an [expect](./test-assertions.md) function to write assertions.
Take a look at the example test below to see how to write a test using web first assertions, locators and selectors.
Take a look at the example test below to see how to write a test using [locators](/locators.md) and web first assertions.
```python
import re
@ -19,7 +19,7 @@ def test_homepage_has_Playwright_in_title_and_get_started_link_linking_to_the_in
expect(page).to_have_title(re.compile("Playwright"))
# create a locator
get_started = page.locator("text=Get Started")
get_started = page.get_by_role("link", name="Get started");
# Expect an attribute "to be strictly equal" to the value.
expect(get_started).to_have_attribute("href", "/docs/intro")
@ -46,27 +46,17 @@ expect(page).to_have_title(re.compile("Playwright"))
### Locators
[Locators](./locators.md) are the central piece of Playwright's auto-waiting and retry-ability. Locators represent a way to find element(s) on the page at any moment and are used to perform actions on elements such as `.click` `.fill` etc. Custom locators can be created with the [`method: Page.locator`] method.
[Locators](./locators.md) are the central piece of Playwright's auto-waiting and retry-ability. Locators represent a way to find element(s) on the page at any moment and are used to perform actions on elements such as `.click` `.fill` etc.
```python
from playwright.sync_api import expect
get_started = page.locator("text=Get Started")
get_started = page.get_by_role("link", name="Get started");
expect(get_started).to_have_attribute("href", "/docs/installation")
get_started.click()
```
[Selectors](./selectors.md) are strings that are used to create Locators. Playwright supports many different selectors like [Text](./selectors.md#text-selector), [CSS](./selectors.md#css-selector), [XPath](./selectors.md#xpath-selectors) and many more. Learn more about available selectors and how to pick one in this [in-depth guide](./selectors.md).
```python
from playwright.sync_api import expect
expect(page.locator("text=Installation")).to_be_visible()
```
### Test Isolation
The Playwright Pytest plugin is based on the concept of test fixtures such as the [built in page fixture](./test-runners.md), which is passed into your test. Pages are isolated between tests due to the Browser Context, which is equivalent to a brand new browser profile, where every test gets a fresh environment, even when multiple tests run in a single Browser.

View file

@ -2458,7 +2458,7 @@ export interface Page {
getByAltText(text: string|RegExp, options?: {
/**
* Whether to find an exact match: case-sensitive and whole-string. Default to false. Ignored when locating by a regular
* expression.
* expression. Note that exact match still trims whitespace.
*/
exact?: boolean;
}): Locator;
@ -2478,7 +2478,7 @@ export interface Page {
getByLabel(text: string|RegExp, options?: {
/**
* Whether to find an exact match: case-sensitive and whole-string. Default to false. Ignored when locating by a regular
* expression.
* expression. Note that exact match still trims whitespace.
*/
exact?: boolean;
}): Locator;
@ -2497,7 +2497,7 @@ export interface Page {
getByPlaceholder(text: string|RegExp, options?: {
/**
* Whether to find an exact match: case-sensitive and whole-string. Default to false. Ignored when locating by a regular
* expression.
* expression. Note that exact match still trims whitespace.
*/
exact?: boolean;
}): Locator;
@ -2586,14 +2586,46 @@ export interface Page {
getByTestId(testId: string): Locator;
/**
* Allows locating elements that contain given text.
* Allows locating elements that contain given text. Consider the following DOM structure:
*
* ```html
* <div>Hello <span>world</span></div>
* <div>Hello</div>
* ```
*
* You can locate by text substring, exact string, or a regular expression:
*
* ```js
* // Matches <span>
* page.getByText('world')
*
* // Matches first <div>
* page.getByText('Hello world')
*
* // Matches second <div>
* page.getByText('Hello', { exact: true })
*
* // Matches both <div>s
* page.getByText(/Hello/)
*
* // Matches second <div>
* page.getByText(/^hello$/i)
* ```
*
* See also [locator.filter([options])](https://playwright.dev/docs/api/class-locator#locator-filter) that allows to match
* by another criteria, like an accessible role, and then filter by the text content.
*
* > NOTE: Matching by text always normalizes whitespace, even with exact match. For example, it turns multiple spaces into
* one, turns line breaks into spaces and ignores leading and trailing whitespace.
* > NOTE: Input elements of the type `button` and `submit` are matched by their `value` instead of the text content. For
* example, locating by text `"Log in"` matches `<input type=button value="Log in">`.
* @param text Text to locate the element for.
* @param options
*/
getByText(text: string|RegExp, options?: {
/**
* Whether to find an exact match: case-sensitive and whole-string. Default to false. Ignored when locating by a regular
* expression.
* expression. Note that exact match still trims whitespace.
*/
exact?: boolean;
}): Locator;
@ -2611,7 +2643,7 @@ export interface Page {
getByTitle(text: string|RegExp, options?: {
/**
* Whether to find an exact match: case-sensitive and whole-string. Default to false. Ignored when locating by a regular
* expression.
* expression. Note that exact match still trims whitespace.
*/
exact?: boolean;
}): Locator;
@ -5549,7 +5581,7 @@ export interface Frame {
getByAltText(text: string|RegExp, options?: {
/**
* Whether to find an exact match: case-sensitive and whole-string. Default to false. Ignored when locating by a regular
* expression.
* expression. Note that exact match still trims whitespace.
*/
exact?: boolean;
}): Locator;
@ -5569,7 +5601,7 @@ export interface Frame {
getByLabel(text: string|RegExp, options?: {
/**
* Whether to find an exact match: case-sensitive and whole-string. Default to false. Ignored when locating by a regular
* expression.
* expression. Note that exact match still trims whitespace.
*/
exact?: boolean;
}): Locator;
@ -5588,7 +5620,7 @@ export interface Frame {
getByPlaceholder(text: string|RegExp, options?: {
/**
* Whether to find an exact match: case-sensitive and whole-string. Default to false. Ignored when locating by a regular
* expression.
* expression. Note that exact match still trims whitespace.
*/
exact?: boolean;
}): Locator;
@ -5677,14 +5709,46 @@ export interface Frame {
getByTestId(testId: string): Locator;
/**
* Allows locating elements that contain given text.
* Allows locating elements that contain given text. Consider the following DOM structure:
*
* ```html
* <div>Hello <span>world</span></div>
* <div>Hello</div>
* ```
*
* You can locate by text substring, exact string, or a regular expression:
*
* ```js
* // Matches <span>
* page.getByText('world')
*
* // Matches first <div>
* page.getByText('Hello world')
*
* // Matches second <div>
* page.getByText('Hello', { exact: true })
*
* // Matches both <div>s
* page.getByText(/Hello/)
*
* // Matches second <div>
* page.getByText(/^hello$/i)
* ```
*
* See also [locator.filter([options])](https://playwright.dev/docs/api/class-locator#locator-filter) that allows to match
* by another criteria, like an accessible role, and then filter by the text content.
*
* > NOTE: Matching by text always normalizes whitespace, even with exact match. For example, it turns multiple spaces into
* one, turns line breaks into spaces and ignores leading and trailing whitespace.
* > NOTE: Input elements of the type `button` and `submit` are matched by their `value` instead of the text content. For
* example, locating by text `"Log in"` matches `<input type=button value="Log in">`.
* @param text Text to locate the element for.
* @param options
*/
getByText(text: string|RegExp, options?: {
/**
* Whether to find an exact match: case-sensitive and whole-string. Default to false. Ignored when locating by a regular
* expression.
* expression. Note that exact match still trims whitespace.
*/
exact?: boolean;
}): Locator;
@ -5702,7 +5766,7 @@ export interface Frame {
getByTitle(text: string|RegExp, options?: {
/**
* Whether to find an exact match: case-sensitive and whole-string. Default to false. Ignored when locating by a regular
* expression.
* expression. Note that exact match still trims whitespace.
*/
exact?: boolean;
}): Locator;
@ -9977,7 +10041,7 @@ export interface Locator {
getByAltText(text: string|RegExp, options?: {
/**
* Whether to find an exact match: case-sensitive and whole-string. Default to false. Ignored when locating by a regular
* expression.
* expression. Note that exact match still trims whitespace.
*/
exact?: boolean;
}): Locator;
@ -9997,7 +10061,7 @@ export interface Locator {
getByLabel(text: string|RegExp, options?: {
/**
* Whether to find an exact match: case-sensitive and whole-string. Default to false. Ignored when locating by a regular
* expression.
* expression. Note that exact match still trims whitespace.
*/
exact?: boolean;
}): Locator;
@ -10016,7 +10080,7 @@ export interface Locator {
getByPlaceholder(text: string|RegExp, options?: {
/**
* Whether to find an exact match: case-sensitive and whole-string. Default to false. Ignored when locating by a regular
* expression.
* expression. Note that exact match still trims whitespace.
*/
exact?: boolean;
}): Locator;
@ -10105,14 +10169,46 @@ export interface Locator {
getByTestId(testId: string): Locator;
/**
* Allows locating elements that contain given text.
* Allows locating elements that contain given text. Consider the following DOM structure:
*
* ```html
* <div>Hello <span>world</span></div>
* <div>Hello</div>
* ```
*
* You can locate by text substring, exact string, or a regular expression:
*
* ```js
* // Matches <span>
* page.getByText('world')
*
* // Matches first <div>
* page.getByText('Hello world')
*
* // Matches second <div>
* page.getByText('Hello', { exact: true })
*
* // Matches both <div>s
* page.getByText(/Hello/)
*
* // Matches second <div>
* page.getByText(/^hello$/i)
* ```
*
* See also [locator.filter([options])](https://playwright.dev/docs/api/class-locator#locator-filter) that allows to match
* by another criteria, like an accessible role, and then filter by the text content.
*
* > NOTE: Matching by text always normalizes whitespace, even with exact match. For example, it turns multiple spaces into
* one, turns line breaks into spaces and ignores leading and trailing whitespace.
* > NOTE: Input elements of the type `button` and `submit` are matched by their `value` instead of the text content. For
* example, locating by text `"Log in"` matches `<input type=button value="Log in">`.
* @param text Text to locate the element for.
* @param options
*/
getByText(text: string|RegExp, options?: {
/**
* Whether to find an exact match: case-sensitive and whole-string. Default to false. Ignored when locating by a regular
* expression.
* expression. Note that exact match still trims whitespace.
*/
exact?: boolean;
}): Locator;
@ -10130,7 +10226,7 @@ export interface Locator {
getByTitle(text: string|RegExp, options?: {
/**
* Whether to find an exact match: case-sensitive and whole-string. Default to false. Ignored when locating by a regular
* expression.
* expression. Note that exact match still trims whitespace.
*/
exact?: boolean;
}): Locator;
@ -15237,7 +15333,7 @@ export interface FrameLocator {
getByAltText(text: string|RegExp, options?: {
/**
* Whether to find an exact match: case-sensitive and whole-string. Default to false. Ignored when locating by a regular
* expression.
* expression. Note that exact match still trims whitespace.
*/
exact?: boolean;
}): Locator;
@ -15257,7 +15353,7 @@ export interface FrameLocator {
getByLabel(text: string|RegExp, options?: {
/**
* Whether to find an exact match: case-sensitive and whole-string. Default to false. Ignored when locating by a regular
* expression.
* expression. Note that exact match still trims whitespace.
*/
exact?: boolean;
}): Locator;
@ -15276,7 +15372,7 @@ export interface FrameLocator {
getByPlaceholder(text: string|RegExp, options?: {
/**
* Whether to find an exact match: case-sensitive and whole-string. Default to false. Ignored when locating by a regular
* expression.
* expression. Note that exact match still trims whitespace.
*/
exact?: boolean;
}): Locator;
@ -15365,14 +15461,46 @@ export interface FrameLocator {
getByTestId(testId: string): Locator;
/**
* Allows locating elements that contain given text.
* Allows locating elements that contain given text. Consider the following DOM structure:
*
* ```html
* <div>Hello <span>world</span></div>
* <div>Hello</div>
* ```
*
* You can locate by text substring, exact string, or a regular expression:
*
* ```js
* // Matches <span>
* page.getByText('world')
*
* // Matches first <div>
* page.getByText('Hello world')
*
* // Matches second <div>
* page.getByText('Hello', { exact: true })
*
* // Matches both <div>s
* page.getByText(/Hello/)
*
* // Matches second <div>
* page.getByText(/^hello$/i)
* ```
*
* See also [locator.filter([options])](https://playwright.dev/docs/api/class-locator#locator-filter) that allows to match
* by another criteria, like an accessible role, and then filter by the text content.
*
* > NOTE: Matching by text always normalizes whitespace, even with exact match. For example, it turns multiple spaces into
* one, turns line breaks into spaces and ignores leading and trailing whitespace.
* > NOTE: Input elements of the type `button` and `submit` are matched by their `value` instead of the text content. For
* example, locating by text `"Log in"` matches `<input type=button value="Log in">`.
* @param text Text to locate the element for.
* @param options
*/
getByText(text: string|RegExp, options?: {
/**
* Whether to find an exact match: case-sensitive and whole-string. Default to false. Ignored when locating by a regular
* expression.
* expression. Note that exact match still trims whitespace.
*/
exact?: boolean;
}): Locator;
@ -15390,7 +15518,7 @@ export interface FrameLocator {
getByTitle(text: string|RegExp, options?: {
/**
* Whether to find an exact match: case-sensitive and whole-string. Default to false. Ignored when locating by a regular
* expression.
* expression. Note that exact match still trims whitespace.
*/
exact?: boolean;
}): Locator;