2021-01-01 15:17:27 -08:00
|
|
|
---
|
|
|
|
id: navigations
|
|
|
|
title: "Navigations"
|
|
|
|
---
|
2020-12-30 18:04:51 -08:00
|
|
|
|
2021-01-14 15:01:39 -08:00
|
|
|
Playwright can navigate to URLs and handle navigations caused by page interactions. This guide covers common scenarios
|
|
|
|
to wait for page navigations and loading to complete.
|
2020-12-30 18:04:51 -08:00
|
|
|
|
2021-01-01 15:17:27 -08:00
|
|
|
<!-- TOC -->
|
2020-12-30 18:04:51 -08:00
|
|
|
|
|
|
|
## Navigation lifecycle
|
|
|
|
|
2021-01-14 15:01:39 -08:00
|
|
|
Playwright splits the process of showing a new document in a page into **navigation** and **loading**.
|
2020-12-30 18:04:51 -08:00
|
|
|
|
2021-11-03 15:26:25 -07:00
|
|
|
**Navigation starts** by changing the page URL or by interacting with the page (e.g., clicking a link).
|
|
|
|
The navigation intent may be canceled, for example, on hitting an unresolved DNS address or transformed into a file download.
|
|
|
|
|
|
|
|
**Navigation is committed** when the response headers have been parsed and session history is updated. Only after the
|
|
|
|
navigation succeeds (is committed), the page starts **loading** the document.
|
2020-12-30 18:04:51 -08:00
|
|
|
|
2021-01-14 15:01:39 -08:00
|
|
|
**Loading** covers getting the remaining response body over the network, parsing, executing the scripts and firing load
|
|
|
|
events:
|
2020-12-30 18:04:51 -08:00
|
|
|
- [`method: Page.url`] is set to the new url
|
|
|
|
- document content is loaded over network and parsed
|
2021-02-04 19:34:09 +01:00
|
|
|
- [`event: Page.DOMContentLoaded`] event is fired
|
2020-12-30 18:04:51 -08:00
|
|
|
- page executes some scripts and loads resources like stylesheets and images
|
|
|
|
- [`event: Page.load`] event is fired
|
|
|
|
- page executes dynamically loaded scripts
|
|
|
|
- `networkidle` is fired when no new network requests are made for 500 ms
|
|
|
|
|
|
|
|
## Scenarios initiated by browser UI
|
2021-01-14 15:01:39 -08:00
|
|
|
|
2020-12-30 18:04:51 -08:00
|
|
|
Navigations can be initiated by changing the URL bar, reloading the page or going back or forward in session history.
|
|
|
|
|
|
|
|
### Auto-wait
|
2021-01-14 15:01:39 -08:00
|
|
|
|
|
|
|
Navigating to a URL auto-waits for the page to fire the `load` event. If the page does a client-side redirect before
|
2021-08-19 12:23:35 -07:00
|
|
|
`load`, [`method: Page.goto`] will auto-wait for the redirected page to fire the `load` event.
|
2020-12-30 18:04:51 -08:00
|
|
|
|
|
|
|
```js
|
|
|
|
// Navigate the page
|
|
|
|
await page.goto('https://example.com');
|
|
|
|
```
|
|
|
|
|
2021-03-01 09:18:44 -08:00
|
|
|
```java
|
|
|
|
// Navigate the page
|
|
|
|
page.navigate("https://example.com");
|
|
|
|
```
|
|
|
|
|
2021-01-14 15:01:39 -08:00
|
|
|
```python async
|
|
|
|
# Navigate the page
|
|
|
|
await page.goto("https://example.com")
|
|
|
|
```
|
|
|
|
|
|
|
|
```python sync
|
|
|
|
# Navigate the page
|
|
|
|
page.goto("https://example.com")
|
|
|
|
```
|
|
|
|
|
2021-05-14 23:22:30 -07:00
|
|
|
```csharp
|
|
|
|
// Navigate the page
|
|
|
|
await page.GotoAsync("https://example.com");
|
|
|
|
```
|
|
|
|
|
|
|
|
|
2020-12-30 18:04:51 -08:00
|
|
|
### Custom wait
|
2021-01-14 15:01:39 -08:00
|
|
|
|
2020-12-30 18:04:51 -08:00
|
|
|
Override the default behavior to wait until a specific event, like `networkidle`.
|
|
|
|
|
|
|
|
```js
|
|
|
|
// Navigate and wait until network is idle
|
|
|
|
await page.goto('https://example.com', { waitUntil: 'networkidle' });
|
|
|
|
```
|
|
|
|
|
2021-03-01 09:18:44 -08:00
|
|
|
```java
|
|
|
|
// Navigate and wait until network is idle
|
|
|
|
page.navigate("https://example.com", new Page.NavigateOptions()
|
2021-03-05 13:50:34 -08:00
|
|
|
.setWaitUntil(WaitUntilState.NETWORKIDLE));
|
2021-03-01 09:18:44 -08:00
|
|
|
```
|
|
|
|
|
2021-01-14 15:01:39 -08:00
|
|
|
```python async
|
|
|
|
# Navigate and wait until network is idle
|
|
|
|
await page.goto("https://example.com", wait_until="networkidle")
|
|
|
|
```
|
|
|
|
|
|
|
|
```python sync
|
|
|
|
# Navigate and wait until network is idle
|
|
|
|
page.goto("https://example.com", wait_until="networkidle")
|
|
|
|
```
|
|
|
|
|
2021-05-14 23:22:30 -07:00
|
|
|
```csharp
|
|
|
|
// Navigate and wait until network is idle
|
2021-05-19 17:19:25 -07:00
|
|
|
await page.GotoAsync("https://example.com", new PageGotoOptions { WaitUntil = WaitUntilState.NetworkIdle });
|
2021-05-14 23:22:30 -07:00
|
|
|
```
|
|
|
|
|
2020-12-30 18:04:51 -08:00
|
|
|
### Wait for element
|
2021-01-14 15:01:39 -08:00
|
|
|
|
2021-11-10 11:30:25 -08:00
|
|
|
In lazy-loaded pages, it can be useful to wait until an element is visible with [`method: Locator.waitFor`].
|
2021-01-14 15:01:39 -08:00
|
|
|
Alternatively, page interactions like [`method: Page.click`] auto-wait for elements.
|
2020-12-30 18:04:51 -08:00
|
|
|
|
|
|
|
```js
|
|
|
|
// Navigate and wait for element
|
|
|
|
await page.goto('https://example.com');
|
2021-11-10 11:30:25 -08:00
|
|
|
await page.locator('text=Example Domain').waitFor();
|
2020-12-30 18:04:51 -08:00
|
|
|
|
|
|
|
// Navigate and click element
|
|
|
|
// Click will auto-wait for the element
|
|
|
|
await page.goto('https://example.com');
|
|
|
|
await page.click('text=Example Domain');
|
|
|
|
```
|
|
|
|
|
2021-03-01 09:18:44 -08:00
|
|
|
```java
|
|
|
|
// Navigate and wait for element
|
|
|
|
page.navigate("https://example.com");
|
2021-11-10 11:30:25 -08:00
|
|
|
page.locator("text=Example Domain").waitFor();
|
2021-03-01 09:18:44 -08:00
|
|
|
|
|
|
|
// Navigate and click element
|
|
|
|
// Click will auto-wait for the element
|
|
|
|
page.navigate("https://example.com");
|
|
|
|
page.click("text=Example Domain");
|
|
|
|
```
|
|
|
|
|
2021-01-14 15:01:39 -08:00
|
|
|
```python async
|
|
|
|
# Navigate and wait for element
|
|
|
|
await page.goto("https://example.com")
|
2021-11-10 11:30:25 -08:00
|
|
|
await page.locator("text=example domain").wait_for()
|
2021-01-14 15:01:39 -08:00
|
|
|
|
|
|
|
# Navigate and click element
|
|
|
|
# Click will auto-wait for the element
|
|
|
|
await page.goto("https://example.com")
|
|
|
|
await page.click("text=example domain")
|
|
|
|
```
|
|
|
|
|
|
|
|
```python sync
|
|
|
|
# Navigate and wait for element
|
|
|
|
page.goto("https://example.com")
|
2021-11-10 11:30:25 -08:00
|
|
|
page.locator("text=example domain").wait_for()
|
2021-01-14 15:01:39 -08:00
|
|
|
|
|
|
|
# Navigate and click element
|
|
|
|
# Click will auto-wait for the element
|
|
|
|
page.goto("https://example.com")
|
|
|
|
page.click("text=example domain")
|
|
|
|
```
|
|
|
|
|
2021-05-14 23:22:30 -07:00
|
|
|
```csharp
|
|
|
|
// Navigate and wait for element
|
|
|
|
await page.GotoAsync("https://example.com");
|
2021-11-10 11:30:25 -08:00
|
|
|
await page.Locator("text=Example Domain").WaitForAsync();
|
2021-05-14 23:22:30 -07:00
|
|
|
|
|
|
|
// Navigate and click element
|
|
|
|
// Click will auto-wait for the element
|
|
|
|
await page.GotoAsync("https://example.com");
|
|
|
|
await page.ClickAsync("text=Example Domain");
|
|
|
|
```
|
|
|
|
|
2021-01-17 21:09:40 -08:00
|
|
|
### API reference
|
2020-12-30 18:04:51 -08:00
|
|
|
- [`method: Page.goto`]
|
|
|
|
- [`method: Page.reload`]
|
|
|
|
- [`method: Page.goBack`]
|
|
|
|
- [`method: Page.goForward`]
|
|
|
|
|
|
|
|
## Scenarios initiated by page interaction
|
2021-01-14 15:01:39 -08:00
|
|
|
|
|
|
|
In the scenarios below, [`method: Page.click`] initiates a navigation and then waits for the navigation to complete.
|
2020-12-30 18:04:51 -08:00
|
|
|
|
|
|
|
### Auto-wait
|
2021-01-14 15:01:39 -08:00
|
|
|
|
|
|
|
By default, [`method: Page.click`] will wait for the navigation step to complete. This can be combined with a page interaction on
|
|
|
|
the navigated page which would auto-wait for an element.
|
2020-12-30 18:04:51 -08:00
|
|
|
|
|
|
|
```js
|
|
|
|
// Click will auto-wait for navigation to complete
|
|
|
|
await page.click('text=Login');
|
2021-03-01 09:18:44 -08:00
|
|
|
|
2020-12-30 18:04:51 -08:00
|
|
|
// Fill will auto-wait for element on navigated page
|
|
|
|
await page.fill('#username', 'John Doe');
|
|
|
|
```
|
|
|
|
|
2021-03-01 09:18:44 -08:00
|
|
|
```java
|
|
|
|
// Click will auto-wait for navigation to complete
|
|
|
|
page.click("text=Login");
|
|
|
|
|
|
|
|
// Fill will auto-wait for element on navigated page
|
|
|
|
page.fill("#username", "John Doe");
|
|
|
|
```
|
|
|
|
|
2021-01-14 15:01:39 -08:00
|
|
|
```python async
|
|
|
|
# Click will auto-wait for navigation to complete
|
|
|
|
await page.click("text=Login")
|
|
|
|
|
|
|
|
# Fill will auto-wait for element on navigated page
|
|
|
|
await page.fill("#username", "John Doe")
|
|
|
|
```
|
|
|
|
|
|
|
|
```python sync
|
|
|
|
# Click will auto-wait for navigation to complete
|
|
|
|
page.click("text=Login")
|
|
|
|
|
|
|
|
# Fill will auto-wait for element on navigated page
|
|
|
|
page.fill("#username", "John Doe")
|
|
|
|
```
|
|
|
|
|
2021-05-14 23:22:30 -07:00
|
|
|
```csharp
|
|
|
|
// Click will auto-wait for navigation to complete
|
|
|
|
await page.ClickAsync("text=Login");
|
|
|
|
|
|
|
|
// Fill will auto-wait for element on navigated page
|
|
|
|
await page.FillAsync("#username", "John Doe");
|
|
|
|
```
|
|
|
|
|
2020-12-30 18:04:51 -08:00
|
|
|
### Custom wait
|
2021-01-14 15:01:39 -08:00
|
|
|
|
2020-12-30 18:04:51 -08:00
|
|
|
`page.click` can be combined with [`method: Page.waitForLoadState`] to wait for a loading event.
|
|
|
|
|
|
|
|
```js
|
|
|
|
await page.click('button'); // Click triggers navigation
|
|
|
|
await page.waitForLoadState('networkidle'); // This resolves after 'networkidle'
|
|
|
|
```
|
|
|
|
|
2021-03-01 09:18:44 -08:00
|
|
|
```java
|
|
|
|
page.click("button"); // Click triggers navigation
|
|
|
|
page.waitForLoadState(LoadState.NETWORKIDLE); // This resolves after "networkidle"
|
|
|
|
```
|
|
|
|
|
2021-01-14 15:01:39 -08:00
|
|
|
```python async
|
|
|
|
await page.click("button"); # Click triggers navigation
|
|
|
|
await page.wait_for_load_state("networkidle"); # This waits for the "networkidle"
|
|
|
|
```
|
|
|
|
|
|
|
|
```python sync
|
|
|
|
page.click("button"); # Click triggers navigation
|
|
|
|
page.wait_for_load_state("networkidle"); # This waits for the "networkidle"
|
|
|
|
```
|
|
|
|
|
2021-05-14 23:22:30 -07:00
|
|
|
```csharp
|
|
|
|
await page.ClickAsync("button"); // Click triggers navigation
|
|
|
|
await page.WaitForLoadStateAsync(LoadState.NetworkIdle); // This resolves after "networkidle"
|
|
|
|
```
|
|
|
|
|
2020-12-30 18:04:51 -08:00
|
|
|
### Wait for element
|
2021-01-14 15:01:39 -08:00
|
|
|
|
2021-11-10 11:30:25 -08:00
|
|
|
In lazy-loaded pages, it can be useful to wait until an element is visible with [`method: Locator.waitFor`].
|
2021-01-14 15:01:39 -08:00
|
|
|
Alternatively, page interactions like [`method: Page.click`] auto-wait for elements.
|
2020-12-30 18:04:51 -08:00
|
|
|
|
|
|
|
```js
|
2021-03-01 09:18:44 -08:00
|
|
|
// Click will auto-wait for the element and trigger navigation
|
2020-12-30 18:04:51 -08:00
|
|
|
await page.click('text=Login');
|
2021-03-01 09:18:44 -08:00
|
|
|
// Wait for the element
|
2021-11-10 11:30:25 -08:00
|
|
|
await page.locator('#username').waitFor();
|
2020-12-30 18:04:51 -08:00
|
|
|
|
|
|
|
// Click triggers navigation
|
|
|
|
await page.click('text=Login');
|
2021-01-14 15:01:39 -08:00
|
|
|
// Fill will auto-wait for element
|
2020-12-30 18:04:51 -08:00
|
|
|
await page.fill('#username', 'John Doe');
|
|
|
|
```
|
|
|
|
|
2021-03-01 09:18:44 -08:00
|
|
|
```java
|
|
|
|
// Click will auto-wait for the element and trigger navigation
|
|
|
|
page.click("text=Login");
|
|
|
|
// Wait for the element
|
2021-11-10 11:30:25 -08:00
|
|
|
page.locator("#username").waitFor();
|
2021-03-01 09:18:44 -08:00
|
|
|
|
|
|
|
// Click triggers navigation
|
|
|
|
page.click("text=Login");
|
|
|
|
// Fill will auto-wait for element
|
|
|
|
page.fill("#username", "John Doe");
|
|
|
|
```
|
|
|
|
|
2021-01-14 15:01:39 -08:00
|
|
|
```python async
|
2021-03-01 09:18:44 -08:00
|
|
|
# Click will auto-wait for the element and trigger navigation
|
2021-01-14 15:01:39 -08:00
|
|
|
await page.click("text=Login")
|
2021-03-01 09:18:44 -08:00
|
|
|
# Wait for the element
|
2021-11-10 11:30:25 -08:00
|
|
|
await page.locator("#username").wait_for()
|
2021-01-14 15:01:39 -08:00
|
|
|
|
|
|
|
# Click triggers navigation
|
|
|
|
await page.click("text=Login")
|
|
|
|
# Fill will auto-wait for element
|
|
|
|
await page.fill("#username", "John Doe")
|
|
|
|
```
|
|
|
|
|
|
|
|
```python sync
|
|
|
|
# Click triggers navigation
|
|
|
|
page.click("text=Login")
|
|
|
|
# Click will auto-wait for the element
|
2021-11-10 11:30:25 -08:00
|
|
|
page.locator("#username").wait_for()
|
2021-01-14 15:01:39 -08:00
|
|
|
|
|
|
|
# Click triggers navigation
|
|
|
|
page.click("text=Login")
|
|
|
|
# Fill will auto-wait for element
|
|
|
|
page.fill("#username", "John Doe")
|
|
|
|
```
|
|
|
|
|
2021-05-14 23:22:30 -07:00
|
|
|
```csharp
|
|
|
|
// Click will auto-wait for the element and trigger navigation
|
|
|
|
await page.ClickAsync("text=Login");
|
|
|
|
// Wait for the element
|
2021-11-10 11:30:25 -08:00
|
|
|
await page.Locator("#username").WaitForAsync();
|
2021-05-14 23:22:30 -07:00
|
|
|
|
|
|
|
// Click triggers navigation
|
|
|
|
await page.ClickAsync("text=Login");
|
|
|
|
// Fill will auto-wait for element
|
|
|
|
await page.FillAsync("#username", "John Doe");
|
|
|
|
```
|
|
|
|
|
2020-12-30 18:04:51 -08:00
|
|
|
### Asynchronous navigation
|
2021-01-14 15:01:39 -08:00
|
|
|
|
2021-01-15 16:01:41 -08:00
|
|
|
Clicking an element could trigger asynchronous processing before initiating the navigation. In these cases, it is
|
2021-01-14 15:01:39 -08:00
|
|
|
recommended to explicitly call [`method: Page.waitForNavigation`]. For example:
|
2020-12-30 18:04:51 -08:00
|
|
|
* Navigation is triggered from a `setTimeout`
|
|
|
|
* Page waits for network requests before navigation
|
|
|
|
|
|
|
|
```js
|
2021-01-14 15:01:39 -08:00
|
|
|
// Note that Promise.all prevents a race condition
|
|
|
|
// between clicking and waiting for a navigation.
|
2020-12-30 18:04:51 -08:00
|
|
|
await Promise.all([
|
2022-01-13 10:38:22 -08:00
|
|
|
// Waits for the next navigation.
|
|
|
|
// It is important to call waitForNavigation before click to set up waiting.
|
|
|
|
page.waitForNavigation(),
|
|
|
|
// Triggers a navigation after a timeout.
|
|
|
|
page.locator('div.delayed-navigation').clikc(),
|
2020-12-30 18:04:51 -08:00
|
|
|
]);
|
|
|
|
```
|
|
|
|
|
2021-03-01 09:18:44 -08:00
|
|
|
```java
|
|
|
|
// Using waitForNavigation with a callback prevents a race condition
|
|
|
|
// between clicking and waiting for a navigation.
|
|
|
|
page.waitForNavigation(() -> { // Waits for the next navigation
|
2021-03-08 12:21:34 -08:00
|
|
|
page.click("div.delayed-navigation"); // Triggers a navigation after a timeout
|
2021-03-01 09:18:44 -08:00
|
|
|
});
|
|
|
|
```
|
|
|
|
|
2021-01-14 15:01:39 -08:00
|
|
|
```python async
|
|
|
|
# Waits for the next navigation. Using Python context manager
|
|
|
|
# prevents a race condition between clicking and waiting for a navigation.
|
|
|
|
async with page.expect_navigation():
|
|
|
|
# Triggers a navigation after a timeout
|
2021-03-08 12:21:34 -08:00
|
|
|
await page.click("div.delayed-navigation")
|
2021-01-14 15:01:39 -08:00
|
|
|
```
|
|
|
|
|
|
|
|
```python sync
|
|
|
|
# Waits for the next navigation. Using Python context manager
|
|
|
|
# prevents a race condition between clicking and waiting for a navigation.
|
|
|
|
with page.expect_navigation():
|
|
|
|
# Triggers a navigation after a timeout
|
|
|
|
page.click("a")
|
|
|
|
```
|
2020-12-30 18:04:51 -08:00
|
|
|
|
2021-05-14 23:22:30 -07:00
|
|
|
```csharp
|
|
|
|
// Using waitForNavigation with a callback prevents a race condition
|
|
|
|
// between clicking and waiting for a navigation.
|
2021-05-26 15:50:42 -07:00
|
|
|
await page.RunAndWaitForNavigationAsync(async () =>
|
|
|
|
{
|
|
|
|
// Triggers a navigation after a timeout
|
|
|
|
await page.ClickAsync("div.delayed-navigation");
|
|
|
|
});
|
2021-05-14 23:22:30 -07:00
|
|
|
```
|
|
|
|
|
2020-12-30 18:04:51 -08:00
|
|
|
### Multiple navigations
|
2021-01-14 15:01:39 -08:00
|
|
|
|
|
|
|
Clicking an element could trigger multiple navigations. In these cases, it is recommended to explicitly
|
|
|
|
[`method: Page.waitForNavigation`] to a specific url. For example:
|
2020-12-30 18:04:51 -08:00
|
|
|
* Client-side redirects issued after the `load` event
|
|
|
|
* Multiple pushes to history state
|
|
|
|
|
|
|
|
```js
|
2021-01-14 15:01:39 -08:00
|
|
|
// Note that Promise.all prevents a race condition
|
|
|
|
// between clicking and waiting for a navigation.
|
2020-12-30 18:04:51 -08:00
|
|
|
await Promise.all([
|
2022-01-13 10:38:22 -08:00
|
|
|
// It is important to call waitForNavigation before click to set up waiting.
|
2020-12-30 18:04:51 -08:00
|
|
|
page.waitForNavigation({ url: '**/login' }),
|
2022-01-13 10:38:22 -08:00
|
|
|
// Triggers a navigation with a script redirect.
|
|
|
|
page.locator('text=Click me').click(),
|
2020-12-30 18:04:51 -08:00
|
|
|
]);
|
|
|
|
```
|
|
|
|
|
2021-03-01 09:18:44 -08:00
|
|
|
```java
|
|
|
|
// Running action in the callback of waitForNavigation prevents a race
|
|
|
|
// condition between clicking and waiting for a navigation.
|
2021-03-05 13:50:34 -08:00
|
|
|
page.waitForNavigation(new Page.WaitForNavigationOptions().setUrl("**/login"), () -> {
|
2021-03-01 09:18:44 -08:00
|
|
|
page.click("a"); // Triggers a navigation with a script redirect
|
|
|
|
});
|
|
|
|
```
|
|
|
|
|
2021-01-14 15:01:39 -08:00
|
|
|
```python async
|
|
|
|
# Using Python context manager prevents a race condition
|
|
|
|
# between clicking and waiting for a navigation.
|
|
|
|
async with page.expect_navigation(url="**/login"):
|
|
|
|
# Triggers a navigation with a script redirect
|
|
|
|
await page.click("a")
|
|
|
|
```
|
|
|
|
|
|
|
|
```python sync
|
|
|
|
# Using Python context manager prevents a race condition
|
|
|
|
# between clicking and waiting for a navigation.
|
|
|
|
with page.expect_navigation(url="**/login"):
|
|
|
|
# Triggers a navigation with a script redirect
|
|
|
|
page.click("a")
|
|
|
|
```
|
2020-12-30 18:04:51 -08:00
|
|
|
|
2021-05-14 23:22:30 -07:00
|
|
|
```csharp
|
|
|
|
// Running action in the callback of waitForNavigation prevents a race
|
|
|
|
// condition between clicking and waiting for a navigation.
|
2021-05-26 15:50:42 -07:00
|
|
|
await page.RunAndWaitForNavigationAsync(async () =>
|
|
|
|
{
|
|
|
|
// Triggers a navigation with a script redirect.
|
|
|
|
await page.ClickAsync("a");
|
|
|
|
}, new PageWaitForNavigationOptions
|
|
|
|
{
|
|
|
|
UrlString = "**/login"
|
|
|
|
});
|
2021-05-14 23:22:30 -07:00
|
|
|
```
|
|
|
|
|
2020-12-30 18:04:51 -08:00
|
|
|
### Loading a popup
|
2021-01-14 15:01:39 -08:00
|
|
|
|
|
|
|
When popup is opened, explicitly calling [`method: Page.waitForLoadState`] ensures that popup is loaded to the desired
|
|
|
|
state.
|
2020-12-30 18:04:51 -08:00
|
|
|
|
|
|
|
```js
|
2022-01-13 10:38:22 -08:00
|
|
|
// Note that Promise.all prevents a race condition
|
|
|
|
// between clicking and waiting for the popup.
|
2020-12-30 18:04:51 -08:00
|
|
|
const [ popup ] = await Promise.all([
|
2022-01-13 10:38:22 -08:00
|
|
|
// It is important to call waitForEvent before click to set up waiting.
|
2020-12-30 18:04:51 -08:00
|
|
|
page.waitForEvent('popup'),
|
2022-01-13 10:38:22 -08:00
|
|
|
// Opens popup.
|
|
|
|
page.locator('a[target="_blank"]').click(),
|
2020-12-30 18:04:51 -08:00
|
|
|
]);
|
|
|
|
await popup.waitForLoadState('load');
|
|
|
|
```
|
|
|
|
|
2021-03-01 09:18:44 -08:00
|
|
|
```java
|
|
|
|
Page popup = page.waitForPopup(() -> {
|
|
|
|
page.click("a[target='_blank']"); // Opens popup
|
|
|
|
});
|
|
|
|
popup.waitForLoadState(LoadState.LOAD);
|
|
|
|
```
|
|
|
|
|
2021-01-14 15:01:39 -08:00
|
|
|
```python async
|
|
|
|
async with page.expect_popup() as popup_info:
|
|
|
|
await page.click('a[target="_blank"]') # Opens popup
|
|
|
|
popup = await popup_info.value
|
|
|
|
await popup.wait_for_load_state("load")
|
|
|
|
```
|
|
|
|
|
|
|
|
```python sync
|
|
|
|
with page.expect_popup() as popup_info:
|
|
|
|
page.click('a[target="_blank"]') # Opens popup
|
|
|
|
popup = popup_info.value
|
|
|
|
popup.wait_for_load_state("load")
|
|
|
|
```
|
|
|
|
|
2021-05-14 23:22:30 -07:00
|
|
|
```csharp
|
2021-05-26 15:11:31 -07:00
|
|
|
var popup = await page.RunAndWaitForPopupAsync(async () =>
|
2021-05-19 17:19:25 -07:00
|
|
|
{
|
|
|
|
await page.ClickAsync("a[target='_blank']"); // Opens popup
|
|
|
|
});
|
2021-05-15 14:02:07 -07:00
|
|
|
popup.WaitForLoadStateAsync(LoadState.Load);
|
2021-05-14 23:22:30 -07:00
|
|
|
```
|
|
|
|
|
2021-01-17 21:09:40 -08:00
|
|
|
### API reference
|
2020-12-30 18:04:51 -08:00
|
|
|
- [`method: Page.click`]
|
|
|
|
- [`method: Page.waitForLoadState`]
|
|
|
|
- [`method: Page.waitForNavigation`]
|
|
|
|
- [`method: Page.waitForFunction`]
|
|
|
|
|
|
|
|
## Advanced patterns
|
2021-01-14 15:01:39 -08:00
|
|
|
|
|
|
|
For pages that have complicated loading patterns, [`method: Page.waitForFunction`] is a powerful and extensible approach
|
|
|
|
to define a custom wait criteria.
|
2020-12-30 18:04:51 -08:00
|
|
|
|
|
|
|
```js
|
|
|
|
await page.goto('http://example.com');
|
|
|
|
await page.waitForFunction(() => window.amILoadedYet());
|
|
|
|
// Ready to take a screenshot, according to the page itself.
|
|
|
|
await page.screenshot();
|
|
|
|
```
|
|
|
|
|
2021-03-01 09:18:44 -08:00
|
|
|
```java
|
|
|
|
page.navigate("http://example.com");
|
|
|
|
page.waitForFunction("() => window.amILoadedYet()");
|
|
|
|
// Ready to take a screenshot, according to the page itself.
|
|
|
|
page.screenshot();
|
|
|
|
```
|
|
|
|
|
2021-01-14 15:01:39 -08:00
|
|
|
```python async
|
|
|
|
await page.goto("http://example.com")
|
|
|
|
await page.wait_for_function("() => window.amILoadedYet()")
|
|
|
|
# Ready to take a screenshot, according to the page itself.
|
|
|
|
await page.screenshot()
|
|
|
|
```
|
|
|
|
|
|
|
|
```python sync
|
|
|
|
page.goto("http://example.com")
|
|
|
|
page.wait_for_function("() => window.amILoadedYet()")
|
|
|
|
# Ready to take a screenshot, according to the page itself.
|
|
|
|
page.screenshot()
|
|
|
|
```
|
|
|
|
|
2021-05-14 23:22:30 -07:00
|
|
|
```csharp
|
|
|
|
await page.GotoAsync("http://example.com");
|
|
|
|
await page.WaitForFunctionAsync("() => window.amILoadedYet()");
|
|
|
|
// Ready to take a screenshot, according to the page itself.
|
|
|
|
await page.ScreenshotAsync();
|
|
|
|
```
|
|
|
|
|
2021-01-17 21:09:40 -08:00
|
|
|
### API reference
|
2021-01-14 15:01:39 -08:00
|
|
|
- [`method: Page.waitForFunction`]
|