2021-01-01 15:17:27 -08:00
---
id: navigations
title: "Navigations"
---
2020-12-30 18:04:51 -08:00
2022-09-27 17:45:12 +02: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
## 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');
2022-10-03 17:02:46 -07:00
await page.getByText('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');
2022-10-03 17:02:46 -07:00
await page.getByText('Example Domain').click();
2020-12-30 18:04:51 -08:00
```
2021-03-01 09:18:44 -08:00
```java
// Navigate and wait for element
page.navigate("https://example.com");
2022-10-03 17:02:46 -07:00
page.getByText("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");
2022-10-03 17:02:46 -07:00
page.getByText("Example Domain").click();
2021-03-01 09:18:44 -08:00
```
2021-01-14 15:01:39 -08:00
```python async
# Navigate and wait for element
await page.goto("https://example.com")
2022-10-03 17:02:46 -07:00
await page.get_by_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")
2022-10-03 17:02:46 -07:00
await page.get_by_text("example domain").click()
2021-01-14 15:01:39 -08:00
```
```python sync
# Navigate and wait for element
page.goto("https://example.com")
2022-10-03 17:02:46 -07:00
page.get_by_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")
2022-10-03 17:02:46 -07:00
page.get_by_text("example domain").click()
2021-01-14 15:01:39 -08:00
```
2021-05-14 23:22:30 -07:00
```csharp
// Navigate and wait for element
await page.GotoAsync("https://example.com");
2022-10-03 17:02:46 -07:00
await page.GetByText("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");
2022-10-03 17:02:46 -07:00
await page.GetByText("Example Domain").ClickAsync();
2021-05-14 23:22:30 -07:00
```
2020-12-30 18:04:51 -08:00
## Scenarios initiated by page interaction
2021-01-14 15:01:39 -08:00
2022-07-13 11:50:18 +02:00
In the scenarios below, [`method: Locator.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
2022-09-27 17:45:12 +02:00
By default, [`method: Locator.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
2022-10-03 17:02:46 -07:00
await page.getByText('Login').click();
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
2022-10-04 09:29:26 -08:00
await page.getByLabel('User Name').fill('John Doe');
2020-12-30 18:04:51 -08:00
```
2021-03-01 09:18:44 -08:00
```java
// Click will auto-wait for navigation to complete
2022-10-03 17:02:46 -07:00
page.getByText("Login").click();
2021-03-01 09:18:44 -08:00
// Fill will auto-wait for element on navigated page
2022-10-04 09:29:26 -08:00
page.getByLabel("User Name").fill("John Doe");
2021-03-01 09:18:44 -08:00
```
2021-01-14 15:01:39 -08:00
```python async
# Click will auto-wait for navigation to complete
2022-10-03 17:02:46 -07:00
await page.get_by_text("Login").click()
2021-01-14 15:01:39 -08:00
# Fill will auto-wait for element on navigated page
2022-10-04 09:29:26 -08:00
await page.get_by_label("User Name").fill("John Doe")
2021-01-14 15:01:39 -08:00
```
```python sync
# Click will auto-wait for navigation to complete
2022-10-03 17:02:46 -07:00
page.get_by_text("Login").click()
2021-01-14 15:01:39 -08:00
# Fill will auto-wait for element on navigated page
2022-10-04 09:29:26 -08:00
page.get_by_label("User Name").fill("John Doe")
2021-01-14 15:01:39 -08:00
```
2021-05-14 23:22:30 -07:00
```csharp
// Click will auto-wait for navigation to complete
2022-10-03 17:02:46 -07:00
await page.GetByText("Login").ClickAsync();
2021-05-14 23:22:30 -07:00
// Fill will auto-wait for element on navigated page
2022-10-04 09:29:26 -08:00
await page.GetByLabel("User Name").FillAsync("John Doe");
2021-05-14 23:22:30 -07:00
```
2020-12-30 18:04:51 -08:00
### Custom wait
2021-01-14 15:01:39 -08:00
2022-07-13 11:50:18 +02:00
`locator.click` can be combined with [`method: Page.waitForLoadState` ] to wait for a loading event.
2020-12-30 18:04:51 -08:00
```js
2022-10-03 17:02:46 -07:00
await page.getByRole('button').click(); // Click triggers navigation
2020-12-30 18:04:51 -08:00
await page.waitForLoadState('networkidle'); // This resolves after 'networkidle'
```
2021-03-01 09:18:44 -08:00
```java
2022-07-13 11:50:18 +02:00
page.locator("button").click(); // Click triggers navigation
2021-03-01 09:18:44 -08:00
page.waitForLoadState(LoadState.NETWORKIDLE); // This resolves after "networkidle"
```
2021-01-14 15:01:39 -08:00
```python async
2022-11-08 01:28:07 +09:00
await page.locator("button").click() # Click triggers navigation
await page.wait_for_load_state("networkidle") # This waits for the "networkidle"
2021-01-14 15:01:39 -08:00
```
```python sync
2022-11-08 01:28:07 +09:00
page.locator("button").click() # Click triggers navigation
page.wait_for_load_state("networkidle") # This waits for the "networkidle"
2021-01-14 15:01:39 -08:00
```
2021-05-14 23:22:30 -07:00
```csharp
2022-07-13 11:50:18 +02:00
await page.Locator("button").ClickAsync(); // Click triggers navigation
2021-05-14 23:22:30 -07:00
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` ].
2022-07-13 11:50:18 +02:00
Alternatively, page interactions like [`method: Locator.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
2022-10-03 17:02:46 -07:00
await page.getByText('Login').click();
2021-03-01 09:18:44 -08:00
// Wait for the element
2022-10-04 09:29:26 -08:00
await page.getByLabel('User Name').waitFor();
2020-12-30 18:04:51 -08:00
// Click triggers navigation
2022-10-03 17:02:46 -07:00
await page.getByText('Login').click();
2021-01-14 15:01:39 -08:00
// Fill will auto-wait for element
2022-10-04 09:29:26 -08:00
await page.getByLabel('User Name').fill('John Doe');
2020-12-30 18:04:51 -08:00
```
2021-03-01 09:18:44 -08:00
```java
// Click will auto-wait for the element and trigger navigation
2022-10-03 17:02:46 -07:00
page.getByText("Login").click();
2021-03-01 09:18:44 -08:00
// Wait for the element
2022-10-04 09:29:26 -08:00
page.getByLabel("User Name").waitFor();
2021-03-01 09:18:44 -08:00
// Click triggers navigation
2022-10-03 17:02:46 -07:00
page.getByText("Login").click();
2021-03-01 09:18:44 -08:00
// Fill will auto-wait for element
2022-10-04 09:29:26 -08:00
page.getByLabel("User Name").fill("John Doe");
2021-03-01 09:18:44 -08:00
```
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
2022-10-03 17:02:46 -07:00
await page.get_by_text("Login").click()
2021-03-01 09:18:44 -08:00
# Wait for the element
2022-10-04 09:29:26 -08:00
await page.get_by_label("User Name").wait_for()
2021-01-14 15:01:39 -08:00
# Click triggers navigation
2022-10-03 17:02:46 -07:00
await page.get_by_text("Login").click()
2021-01-14 15:01:39 -08:00
# Fill will auto-wait for element
2022-10-04 09:29:26 -08:00
await page.get_by_label("User Name").fill("John Doe")
2021-01-14 15:01:39 -08:00
```
```python sync
# Click triggers navigation
2022-10-03 17:02:46 -07:00
page.get_by_text("Login").click()
2021-01-14 15:01:39 -08:00
# Click will auto-wait for the element
2022-10-04 09:29:26 -08:00
page.get_by_label("User Name").wait_for()
2021-01-14 15:01:39 -08:00
# Click triggers navigation
2022-10-03 17:02:46 -07:00
page.get_by_text("Login").click()
2021-01-14 15:01:39 -08:00
# Fill will auto-wait for element
2022-10-04 09:29:26 -08:00
page.get_by_label("User Name").fill("John Doe")
2021-01-14 15:01:39 -08:00
```
2021-05-14 23:22:30 -07:00
```csharp
// Click will auto-wait for the element and trigger navigation
2022-10-03 17:02:46 -07:00
await page.GetByText("Login").ClickAsync();
2021-05-14 23:22:30 -07:00
// Wait for the element
2022-10-04 09:29:26 -08:00
await page.GetByLabel("User Name").WaitForAsync();
2021-05-14 23:22:30 -07:00
// Click triggers navigation
2022-10-03 17:02:46 -07:00
await page.GetByText("Login").ClickAsync();
2021-05-14 23:22:30 -07:00
// Fill will auto-wait for element
2022-10-04 09:29:26 -08:00
await page.GetByLabel("User Name").FillAsync("John Doe");
2021-05-14 23:22:30 -07:00
```
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.
2022-01-16 04:31:52 +04:00
page.locator('div.delayed-navigation').click(),
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
2022-07-13 11:50:18 +02:00
page.locator("div.delayed-navigation").click(); // 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
2022-07-13 11:50:18 +02:00
await page.locator("div.delayed-navigation").click()
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
2022-07-13 11:50:18 +02:00
page.locator("a").click()
2021-01-14 15:01:39 -08:00
```
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
2022-07-13 11:50:18 +02:00
await page.Locator("div.delayed-navigation").ClickAsync();
2021-05-26 15:50:42 -07:00
});
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.
2022-10-03 17:02:46 -07:00
page.getByText('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"), () -> {
2022-10-03 17:02:46 -07:00
page.getByText("Click me").click(); // Triggers a navigation with a script redirect
2021-03-01 09:18:44 -08:00
});
```
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
2022-10-03 17:02:46 -07:00
await page.get_by_text("Click me").click()
2021-01-14 15:01:39 -08:00
```
```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
2022-10-03 17:02:46 -07:00
page.get_by_text("Click me").click()
2021-01-14 15:01:39 -08:00
```
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.
2022-10-03 17:02:46 -07:00
await page.GetByText("Click me").ClickAsync();
2022-07-13 11:50:18 +02:00
}, new()
2021-05-26 15:50:42 -07:00
{
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
2022-09-27 17:45:12 +02: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(() -> {
2022-07-13 11:50:18 +02:00
page.locator("a[target='_blank']").click(); // Opens popup
2021-03-01 09:18:44 -08:00
});
popup.waitForLoadState(LoadState.LOAD);
```
2021-01-14 15:01:39 -08:00
```python async
async with page.expect_popup() as popup_info:
2022-07-13 11:50:18 +02:00
await page.locator('a[target="_blank"]').click() # Opens popup
2021-01-14 15:01:39 -08:00
popup = await popup_info.value
await popup.wait_for_load_state("load")
```
```python sync
with page.expect_popup() as popup_info:
2022-07-13 11:50:18 +02:00
page.locator('a[target="_blank"]').click() # Opens popup
2021-01-14 15:01:39 -08:00
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
{
2022-07-13 11:50:18 +02:00
await page.Locator("a[target='_blank']").ClickAsync(); // Opens popup
2021-05-19 17:19:25 -07:00
});
2021-05-15 14:02:07 -07:00
popup.WaitForLoadStateAsync(LoadState.Load);
2021-05-14 23:22:30 -07:00
```
2020-12-30 18:04:51 -08:00
## Advanced patterns
2021-01-14 15:01:39 -08:00
2022-09-27 17:45:12 +02: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();
```