Puppeteer. What is waitForNavigation waiting for?

Viewed 961

What "navigation" is waitForNavigation waiting for?

The websites navigation? The browsers refresh icon "navigation" to finish spinning?

Or is this just a awkwardly worded method that should be named waitForBrowserToLoad?


But, when I use it, like so:

await this.page.waitForNavigation();

The page DOES finish loading.

And yet it never resolves. I'm not sure why.

What is waitForNavigation waiting for?

async function beforeScrape(page) {
  //code gets this far
  await this.page.waitForNavigation();
  //never resolves
  await page.click(".table-header");
}
1 Answers

Documentation says:

[page.waitForNavigation] resolves when the page navigates to a new URL or reloads. It is useful for when you run code which will indirectly cause the page to navigate.

For example you fill a form and the click "Submit" button, after which a new page is shown with results. This way you can wait until the new page loaded:

await Promise.all([
  page.waitForNavigation(),
  page.click('input[type=submit]'),
]);

If you instruct the script to wait for navigation, but do not cause one, it will just sit there waiting until timing out.

Related