I’m trying to call waitForNetworkIdle twice in my Puppeteer test, but the second call throws a Timeout cleared error. Here’s the function I’m using:
private async clickEnterButton() {
this.logger('Clicando em entrar')
await this.page.waitForSelector('#submit-button')
await this.page.click('#submit-button')
await this.page.waitForNetworkIdle()
}
I’ve tried calling waitForNetworkIdle outside the function and also changing Node and Puppeteer versions, but the issue persists.
How can I call waitForNetworkIdle multiple times without hitting a timeout?
The Timeout cleared error happens because Puppeteer’s waitForNetworkIdle relies on an internal timer and network listeners. When you call it multiple times, the first call might have already removed the listeners or cleared the timer, so the second call can’t start properly.
A common fix is to always pass a timeout explicitly and ensure a fresh page state:
await this.page.waitForNetworkIdle({ timeout: 5000 });
Alternatively, you can use waitForResponse or waitForRequest for more controlled waits instead of relying on network idle repeatedly.
I ran into this before and solved it by waiting for network idle only after significant navigation or action. Calling waitForNetworkIdle multiple times in quick succession can conflict internally:
await this.page.click(‘#submit-button’);
await this.page.waitForNetworkIdle({ timeout: 5000, idleTime: 500 });
If you need multiple waits, make sure each one corresponds to a distinct network activity rather than reusing it after the previous call finishes. This usually prevents the “Timeout cleared” error.Preformatted text
If your workflow allows it, another approach is to replace waitForNetworkIdle with page.waitForLoadState(‘networkidle’) in Playwright or equivalent Puppeteer logic. It’s often more stable because it handles the timer and event listeners internally without overlapping calls:
await this.page.waitForNavigation({ waitUntil: ‘networkidle0’ });
This way, you can wait for network quiescence multiple times without hitting the internal timeout issue.Preformatted text