question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

waitForNavigation doesn't work after clicking a link

See original GitHub issue

Steps to reproduce

Tell us about your environment:

  • Puppeteer version: 0.13.0
  • Platform / OS version: Win10x64 / Chromium 64.0.3270.0 r516713

What steps will reproduce the problem?

const puppeteer = require('puppeteer');

(async () => {
	try {
		const browser = await puppeteer.launch({
			headless       : false,
			executablePath : 'D:/path/to/chromium/chrome.exe',
			userDataDir    : 'D:/path/to/userdir',
			args           : ['about:blank']
		});

		const pages = await browser.pages();
		const page = pages[0];

		page.on('load', () => console.log('Loaded: ' + page.url()));

		// goto: page1.html
		await page.goto('https://leo020588.bitbucket.io/page1.html', {waitUntil: 'load'});

		// workaround-fix
		/*await page.evaluate(() => {
			let link = document.querySelector('a');
			link.addEventListener('click', (event) => {
				event.preventDefault();
				event.stopPropagation();
				window.location = link.href;
			});
		});*/

		// click: link (page2.html)
		await page.click('a');

		//
		await page.waitForNavigation({waitUntil: 'load'});
		console.log('page2.html has arrived ... the wait is over');

		//
		await browser.close();

	} catch (e) {
		console.log(e);
	}
})();

What is the expected result? page2.html has arrived ... the wait is over

What happens instead? Error: Navigation Timeout Exceeded: 30000ms exceeded

Note If I uncomment the workaround-fix works as espected.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:12
  • Comments:31 (7 by maintainers)

github_iconTop GitHub Comments

38reactions
leo020588commented, Nov 20, 2017

@ebidel Sorry if this is not the place to discuss standard-javascript implementation/understanding. What suggested @vsemozhetbyt works for me. But I still think your suggestion has the same behavior as my code, I would like to clarify if am I wrong for not to confuse other readers before I close the issue.

OPTION-1 (@leo020588) - my original code doesn’t work as expected (Error: Navigation Timeout)

// I read this as:
// - wait for page.click() to resolve, then after resolves ...
// - wait for page.waitForNavigation() to resolve, then after resolves ...
// - console.log
await page.click('a');
await page.waitForNavigation();
console.log('The wait is over 1');

OPTION-2 (@vsemozhetbyt) - works as expected (“The wait is over 2”)

// I read this as:
// - trigger page.click(), don't wait to resolve, inmediately after ...
// - wait for page.waitForNavigation() to resolve, then after resolves ...
// - then console.log"
page.click('a');
await page.waitForNavigation();
console.log('The wait is over 2');

OPTION-3 (@ebidel) - doesn’t work as expected (Error: Navigation Timeout)

// I read this exactly the same as OPTION-1
await page.click('a').then(() => page.waitForNavigation());
console.log('The wait is over 3');

Edit: I like @aslushnikov’s approach better, it makes more sense:

await Promise.all([
  page.waitForNavigation(),
  page.click('a')
]);
36reactions
aslushnikovcommented, Nov 19, 2017

@leo020588 another way to think of waitForNavigation is that it waits for the navigation to start and complete.

In your case, the await page.click('a') returns control to the script too late - the navigation has already started.

The handy way to do this is:

await Promise.all([
  page.click('a'),
  page.waitForNavigation()
]);
Read more comments on GitHub >

github_iconTop Results From Across the Web

waitForNavigation hanging, even though page was loaded
According to Puppeteer documentation proper pattern for clicking on submit and waiting for navigation is like this: await Promise.all([ page ...
Read more >
Page.waitForNavigation() method - Puppeteer
Waits for the page to navigate to a new URL or to reload. It is useful when you run code that will indirectly...
Read more >
Puppeteer documentation - DevDocs
Puppeteer 7.1.0 API documentation with instant search, offline support, keyboard shortcuts, mobile version, and more.
Read more >
Navigations - Playwright 中文文档 - Bootstrap
The navigation intent may be canceled, for example, on hitting an unresolved DNS ... Navigating to a URL auto-waits for the page to...
Read more >
Navigating & waiting - Checkly
waitForXpath() function is you are using XPath selectors instead of CSS selectors. page.waitForNavigation(). In your scripts you can click on a link that ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found