Javascript puppeteer:在继续下一行之前等待 N 秒

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46919013/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 03:40:00  来源:igfitidea点击:

puppeteer: wait N seconds before continuing next line

javascriptnode.jschromiumpuppeteerbrowser-testing

提问by Vivien Pipo

in puppeteerI would like to wait a defined time before going to the next line of code.

puppeteer 中,我想在转到下一行代码之前等待定义的时间。

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

我试图将 asetTimeout放入一个评估函数中,但它似乎被简单地忽略了

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 waitingand after waiting

这段代码不等待,只是在等待之前等待之后

Do you know how to do this?

你知道怎么做吗?

回答by Md. Abu Taher

You can use a little promise function,

你可以使用一点promise函数,

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 waitFor function.

如果您必须使用 puppeteer,请使用内置的 waitFor 函数。

await page.waitFor(4000)

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

如果还想用page.evaluate,4秒后解决。你没有解决任何问题。

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

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

但我想你可以简单地使用前两个例子。

回答by Grant Miller

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 delayoption, which may come in handy for waiting between certain events:

或者,有许多包含内置delay选项的Puppeteer 函数,这可能会在某些事件之间进行等待时派上用场:

// 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});

回答by Huckleberry Carignan

I've been using:

我一直在使用:

await page.waitFor(3000);

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

其中 3000 是毫秒 这似乎对我有用。

回答by Yordan

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();

回答by Xin

await new Promise(_func=> setTimeout(_func, 5000));

回答by atopcu

Your syntax is incomplete.
Try this...

你的语法不完整。
尝试这个...

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