Navigations
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.
- Navigation lifecycle
- Scenarios initiated by browser UI
- Scenarios initiated by page interaction
- Advanced patterns
#
Navigation lifecyclePlaywright splits the process of showing a new document in a page into navigation and loading.
Navigations can be initiated by changing the page URL or by interacting with the page (e.g., clicking a link). Navigation ends when response headers have been parsed and session history is updated. The navigation intent may be canceled, for example, on hitting an unresolved DNS address or transformed into a file download. Only after the navigation succeeds, page starts loading the document.
Loading covers getting the remaining response body over the network, parsing, executing the scripts and firing load events:
- page.url() is set to the new url
- document content is loaded over network and parsed
- page.on('domcontentloaded') event is fired
- page executes some scripts and loads resources like stylesheets and images
- page.on('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 UINavigations can be initiated by changing the URL bar, reloading the page or going back or forward in session history.
#
Auto-waitNavigating to a URL auto-waits for the page to fire the load
event. If the page does a client-side redirect before load
, page.goto(url[, options]) will auto-wait for the redirected page to fire the load
event.
// Navigate the pageawait page.goto('https://example.com');
#
Custom waitOverride the default behavior to wait until a specific event, like networkidle
.
// Navigate and wait until network is idleawait page.goto('https://example.com', { waitUntil: 'networkidle' });
#
Wait for elementIn lazy-loaded pages, it can be useful to wait until an element is visible with page.waitForSelector(selector[, options]). Alternatively, page interactions like page.click(selector[, options]) auto-wait for elements.
// Navigate and wait for elementawait page.goto('https://example.com');await page.waitForSelector('text=Example Domain');
// Navigate and click element// Click will auto-wait for the elementawait page.goto('https://example.com');await page.click('text=Example Domain');
#
API reference#
Scenarios initiated by page interactionIn the scenarios below, page.click(selector[, options]) initiates a navigation and then waits for the navigation to complete.
#
Auto-waitBy default, page.click(selector[, options]) 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.
// Click will auto-wait for navigation to completeawait page.click('text=Login');
// Fill will auto-wait for element on navigated pageawait page.fill('#username', 'John Doe');
#
Custom waitpage.click
can be combined with page.waitForLoadState([state, options]) to wait for a loading event.
await page.click('button'); // Click triggers navigationawait page.waitForLoadState('networkidle'); // This resolves after 'networkidle'
#
Wait for elementIn lazy-loaded pages, it can be useful to wait until an element is visible with page.waitForSelector(selector[, options]). Alternatively, page interactions like page.click(selector[, options]) auto-wait for elements.
// Click will auto-wait for the element and trigger navigationawait page.click('text=Login');// Wait for the elementawait page.waitForSelector('#username');
// Click triggers navigationawait page.click('text=Login');// Fill will auto-wait for elementawait page.fill('#username', 'John Doe');
#
Asynchronous navigationClicking an element could trigger asynchronous processing before initiating the navigation. In these cases, it is recommended to explicitly call page.waitForNavigation([options]). For example:
- Navigation is triggered from a
setTimeout
- Page waits for network requests before navigation
// Note that Promise.all prevents a race condition// between clicking and waiting for a navigation.await Promise.all([ page.waitForNavigation(), // Waits for the next navigation page.click('div.delayed-navigation'), // Triggers a navigation after a timeout]);
#
Multiple navigationsClicking an element could trigger multiple navigations. In these cases, it is recommended to explicitly page.waitForNavigation([options]) to a specific url. For example:
- Client-side redirects issued after the
load
event - Multiple pushes to history state
// Note that Promise.all prevents a race condition// between clicking and waiting for a navigation.await Promise.all([ page.waitForNavigation({ url: '**/login' }), page.click('a'), // Triggers a navigation with a script redirect]);
#
Loading a popupWhen popup is opened, explicitly calling page.waitForLoadState([state, options]) ensures that popup is loaded to the desired state.
const [ popup ] = await Promise.all([ page.waitForEvent('popup'), page.click('a[target="_blank"]'), // Opens popup]);await popup.waitForLoadState('load');
#
API reference- page.click(selector[, options])
- page.waitForLoadState([state, options])
- page.waitForSelector(selector[, options])
- page.waitForNavigation([options])
- page.waitForFunction(pageFunction[, arg, options])
#
Advanced patternsFor pages that have complicated loading patterns, page.waitForFunction(pageFunction[, arg, options]) is a powerful and extensible approach to define a custom wait criteria.
await page.goto('http://example.com');await page.waitForFunction(() => window.amILoadedYet());// Ready to take a screenshot, according to the page itself.await page.screenshot();