
If all else fails and you have to block the script, you can delay, but I've written hundreds of Puppeteer scripts and have never had to use any sleep variant, so I'm pretty convinced it's basically unnecessary. WaitForFunction in particular is underused but it adds a huge amount of reliability and precision that waitForTimeout doesn't. Something else? Run an evaluate block and add your own code to wait for a DOM mutation or poll with setInterval or requestAnimationFrame and effectively reimplement waitForFunction as fits your needs.Are you trying to avoid detection? Try slowMo.Are you waiting for some arbitrary predicate, like a minimum number of child elements to appear? Try waitForFunction.Are you waiting for a popup? Try promisifying page.on("dialog".Are you waiting for a network event or state? Try waitForRequest, waitForResponse or waitForNetworkIdle.Are you waiting for a navigation? Try waitForNavigation or adjust the waitUntil option on goto.Are you waiting for a CSS selector, aria label, text or XPath to appear? Try waitForSelector (optionally with the appropriate built-in selector).There's almost always a better predicate to wait on: Other answers have shown how to sleep, but now that page.waitForTimeout is finally deprecated, I figure I'd add the answer I've wanted to add for awhile:ĭon't sleep! It causes a race condition that disrupts the event-driven nature of Puppeteer, introducing unnecessary brittleness. Defaults to 0.Īwait page.click('#example', ) Time to wait between mousedown and mouseup in milliseconds.

Useful if you're about to stub timers globally, but you still want to use delay to manage your tests.You can use one of the following options to wait for one second: await page.waitFor(1000) Īwait new Promise(r => setTimeout(r, 1000)) Īlternatively, there are many Puppeteer functions that include a built-in delay option, which may come in handy for waiting between certain events: // Click Delay

Const delay = require ( 'delay' ) ( async ( ) => )Ĭreates a new delay instance using the provided functions for clearing and setting timeouts.
