简体   繁体   中英

How to use setTimeout with puppeteer page.evaluate

I was trying to run something every 100 milliseconds in puppeteer using page.evaluate, but when i run the code after setting the timeout nothing happens. I tried running in a non headless browser and even running setTimeout in the dev tools and nothing happened, what should I do to make the timeout actually run?

Here is the relevant code

await page.evaluate(async () => {
await new Promise<void>((resolve) => {
  setTimeout(() => console.log('timeout'), 100)
});
})

EDIT: A bit more data, if i run page.evaluate(() => console.log('a')) it works, and when I run it in a non headless browser I see javascript is blocked

Try this (also, there was a typo in setTiemout in your code):

  await page.evaluate(() => {
    return new Promise((resolve) => {
      setTimeout(() => { console.log('timeout'); resolve(); }, 100);
    });
  });

I managed to fix my issue and it was totally my mistake, if you are having issues of this kind just check that you are not disabling javascript in your code (I should have done that). The way you would be doing it would be page.setJavascriptEnabled(false).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM