For AI agents: a documentation index is available at /llms.txt
Skip to main content

Wait for Navigation After a Click

Clicking a link or button that triggers a page load requires Promise.all to guarantee the navigation wait is registered before the click fires. This pattern is essential for reliable session management with remote browsers.

Prerequisites

Steps

The /function endpoint runs your script inside the browser process, so the same Promise.all pattern applies. Use it to avoid the race condition even here.

View Full Code on GitHub

1. Send the request

curl -X POST \
"https://production-sfo.browserless.io/function?token=YOUR_API_TOKEN_HERE" \
-H "Content-Type: application/javascript" \
--data-raw 'module.exports = async ({ page }) => {
await page.goto("https://example.com");

// Register the navigation wait before clicking — if you click first,
// the page load can complete before waitForNavigation() starts listening.
await Promise.all([
page.waitForNavigation({ waitUntil: "domcontentloaded" }),
page.click("a"),
]);

const heading = await page.$eval("h1", el => el.textContent.trim());
return { url: page.url(), heading };
};'

2. Check the output

{
"url": "https://www.iana.org/domains/reserved",
"heading": "IANA-managed Reserved Domains"
}