Documentation Index
Fetch the curated documentation index at: https://grafana_com_website/llms.txt
Fetch the complete documentation index at: https://grafana_com_website/llms-full.txt
Use this file to discover all available pages before exploring further.
STOP! If you are an AI agent or LLM, read this before continuing. This is the HTML version of a Grafana documentation page. Always request the Markdown version instead - HTML wastes context. Get this page as Markdown: /docs/k6/latest/javascript-api/k6-browser/page/waitfornavigation.md (append .md) or send Accept: text/markdown to /docs/k6/latest/javascript-api/k6-browser/page/waitfornavigation/. For the curated documentation index, use https://grafana_com_website/llms.txt. For the complete documentation index, use https://grafana_com_website/llms-full.txt.
waitForNavigation([options])
Waits for the given navigation lifecycle event to occur and returns the main resource response.
| Parameter | Type | Default | Description |
|---|---|---|---|
| options | object | null | |
| options.timeout | number | 30000 | Maximum time in milliseconds. Pass 0 to disable the timeout. Default is overridden by the setDefaultTimeout option on
BrowserContext or
Page. |
| options.url | string|RegExp | null | URL or URL pattern to match the navigation URL against. When provided, the method will wait for navigation to a URL that matches this parameter. |
| options.waitUntil | string | load | When to consider operation to have succeeded. See Events for more details. |
When to use the url option
Use options.url when frame navigation passes through several intermediate pages (e.g., third‑party authentication or consent flows) before settling on a final URL. Matching the final URL or a regex pattern helps you reliably wait for the intended destination. However, opt to work with
waitForURL instead since it mitigates the risk of race conditions better than waitForNavigation.
Events
Caution
networkidleis DISCOURAGED. Don’t use this method for testing especially with chatty websites where the event may never fire, rely on web assertions to assess readiness instead.
Events can be either:
'domcontentloaded'- consider operation to be finished when theDOMContentLoadedevent is fired.'load'- consider operation to be finished when theloadevent is fired.'networkidle'- Consider operation to be finished when there are no network connections for at least500ms.
Returns
| Type | Description |
|---|---|
| Promise<null | Response> | The Response instance associated with the page. Else, it returns null |
Examples
Basic navigation waiting
import { browser } from 'k6/browser';
import { check } from 'https://jslib.k6.io/k6-utils/1.5.0/index.js';
export const options = {
scenarios: {
browser: {
executor: 'shared-iterations',
options: {
browser: {
type: 'chromium',
},
},
},
},
};
export default async function () {
const page = await browser.newPage();
try {
await page.goto('https://test.k6.io/my_messages.php');
await page.locator('input[name="login"]').type('admin');
await page.locator('input[name="password"]').type('123');
const submitButton = page.locator('input[type="submit"]');
await Promise.all([submitButton.click(), page.waitForNavigation()]);
await check(page.locator('h2'), {
header: async (h2) => (await h2.textContent()) == 'Welcome, admin!',
});
} finally {
await page.close();
}
}Wait for navigation to specific URL
import { browser } from 'k6/browser';
export const options = {
scenarios: {
browser: {
executor: 'shared-iterations',
options: {
browser: {
type: 'chromium',
},
},
},
},
};
export default async function () {
const page = await browser.newPage();
try {
await page.goto('https://quickpizza.grafana.com/test.k6.io/');
// Wait for navigation to a specific URL
await Promise.all([
page.click('a[href="/my_messages.php"]'),
page.waitForNavigation({ url: 'https://quickpizza.grafana.com/my_messages.php' }),
]);
await page.goto('https://quickpizza.grafana.com/test.k6.io/');
// Wait for navigation using URL pattern with RegExp
await Promise.all([
page.click('a[href="/browser.php"]'),
page.waitForNavigation({ url: /\/browser\.php$/ }),
]);
} finally {
await page.close();
}
}Was this page helpful?
Related resources from Grafana Labs

