Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Puppeteer waitForTimeout is not a function

I am using puppeteer in my latest project. I installed the library using NPM (npm i puppeteer). From the documentation I understood that the page.waitFor() function is now deprecated and replaced by page.waitForTimeout().

When I try to use the new function I get UnhandledPromiseRejectionWarning: TypeError: page.waitForTimeout is not a function.

What could be the possible reason? The version of Puppeteer I got from NPM is 2.1.1.

like image 952
Tomáš Nosek Avatar asked Dec 05 '25 16:12

Tomáš Nosek


2 Answers

Since old versions of Puppeteer don't include waitForTimeout and versions >= 22 removed the method, you can use a vanilla Node sleep:

import {setTimeout} from "node:timers/promises";

// ...
await setTimeout(3000);

If you're on a super old version of Node that doesn't have timers/promises, you can always promisify setTimeout yourself, which is a nice pattern to be aware of since it works in the browser:

const sleep = ms => new Promise(res => setTimeout(res, ms));

(async () => {
  console.log(new Date().getSeconds());
  await sleep(3000);
  console.log(new Date().getSeconds());
})();

That said, see this answer for an explanation of why sleeping should be avoided in general in Puppeteer, only used for temporary debugging, as an absolute last resort or in one-off hack scripts.

like image 172
ggorlen Avatar answered Dec 08 '25 04:12

ggorlen


page.waitForTimeout has been introduced in puppeteer version 5.3.0.

So as was mentioned in the comments, you can't use this new function in version 2.1.1 you had installed.

Bonus: Issue #6214 contains more details to this topic.

like image 43
sbocinec Avatar answered Dec 08 '25 06:12

sbocinec



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!