Jinx
Jinx

Reputation: 100

How do I wait for a random amount of time before executing the next action in Puppeteer?

I would love to be able to wait for a random amount of time (let's say a number between 5-12 seconds, chosen at random each time) before executing my next action in Puppeteer, in order to make the behaviour seem more authentic/real world user-like.

I'm aware of how to do it in plain Javascript (as detailed in the Mozilla docs here), but can't seem to get it working in Puppeteer using the waitFor call (which I assume is what I'm supposed to use?).

Any help would be greatly appreciated! :)

Upvotes: 1

Views: 1804

Answers (1)

Md. Abu Taher
Md. Abu Taher

Reputation: 18826

You can use vanila JS to randomly wait between 5-12 seconds between action.

await page.waitFor((Math.floor(Math.random() * 12) + 5) * 1000)

Where:

  • 5 is the start number
  • 12 is the end number
  • 1000 means it's converting seconds to milliseconds

(PS: However, if you question is about waiting 5-12 seconds randomly before every action, then you should have a class with wrapper, which is a different issue until you update your question.)

Upvotes: 5

Related Questions