110

In puppeteer I would like to wait a defined time before going to the next line of code.

I've tried to put a setTimeout in an evaluate function but it seems to be simply ignored

console.log('before waiting');
await page.evaluate(async() => {
  setTimeout(function(){
      console.log('waiting');
  }, 4000)
});
console.log('after waiting');

This code don't wait and just write before waiting and after waiting

Do you know how to do this?

Benjamin
  • 4,920
  • 3
  • 20
  • 43
Pipo
  • 4,530
  • 7
  • 28
  • 60
  • await page.evaluate(async() => { setTimeout(function(){ console.log('waiting'); }, 4000);}); your code is not right , above is the right – Osama Oct 24 '17 at 20:03

7 Answers7

183

You can use a little promise function,

function delay(time) {
   return new Promise(function(resolve) { 
       setTimeout(resolve, time)
   });
}

Then, call it whenever you want a delay.

console.log('before waiting');
await delay(4000);
console.log('after waiting');

If you must use puppeteer use the builtin waitForTimeout function.

await page.waitForTimeout(4000)

If you still want to use page.evaluate, resolve it after 4 seconds. You are not resolving anything.

await page.evaluate(async() => {
    await new Promise(function(resolve) { 
           setTimeout(resolve, 1000)
    });
});

But I guess you can simply use the first two examples.

Md. Abu Taher
  • 15,529
  • 5
  • 45
  • 64
92

I've been using:

await page.waitForTimeout(3000);

Where 3000 is Milliseconds And that seems to be working for me.

Benny Neugebauer
  • 46,191
  • 22
  • 218
  • 190
Huckleberry Carignan
  • 1,566
  • 2
  • 9
  • 27
50

You can use one of the following options to wait for one second:

await page.waitFor(1000);
await frame.waitFor(1000);
await new Promise(r => setTimeout(r, 1000));

Alternatively, there are many Puppeteer functions that include a built-in delay option, which may come in handy for waiting between certain events:

// Click Delay
// Time to wait between mousedown and mouseup in milliseconds. Defaults to 0.

await page.click('#example', {delay: 1000});
await frame.click('#example', {delay: 1000});
await elementHandle.click({delay: 1000});
await page.mouse.click(0, 0, {delay: 1000});

// Type Delay
// Time to wait between key presses in milliseconds. Defaults to 0.

await page.type('#example', 'Hello, world!', {delay: 1000});
await frame.type('#example', 'Hello, world!', {delay: 1000});
await elementHandle.type('Hello, world!', {delay: 1000});
await page.keyboard.type('Hello, world!', {delay: 1000});

// Press Delay
// Time to wait between keydown and keyup in milliseconds. Defaults to 0.

await elementHandle.press('Backspace', {delay: 1000});
await page.keyboard.press('Backspace', {delay: 1000});
Grant Miller
  • 24,187
  • 16
  • 134
  • 150
  • waitFor is deprecated and will be removed. The new function is waitForTimeout https://github.com/puppeteer/puppeteer/issues/6214 – Scott Jodoin Dec 31 '21 at 13:44
23

page.waitFor has now been deprecated.

page.waitForTimeout is now advised to pause script execution for the given number of milliseconds before continuing:

await page.waitForTimeout(1000)
3

Try this function.

function sleep(ms) {
    return new Promise(resolve => setTimeout(resolve, ms));
}

to use it

  async function demo() {
    console.log('Waiting...');
    await sleep(3000);
    console.log('ok');
  }

  demo();
Yordan
  • 68
  • 6
0
await new Promise(_func=> setTimeout(_func, 5000));
Xin
  • 28,603
  • 13
  • 79
  • 74
-4

Your syntax is incomplete.
Try this...

await page.evaluate(async() => {
    setTimeout(function(){
        console.log('waiting');
    }, 4000)
});
atopcu
  • 174
  • 8
  • thank for your help ,i edited the question , sorry for the litle mistake but i really ask how to wait – Pipo Oct 24 '17 at 20:11