簡體   English   中英

puppeteer:在繼續下一行之前等待 N 秒

[英]puppeteer: wait N seconds before continuing to the next line

puppeteer中,我想在進入下一行代碼之前等待一段定義的時間。

我試圖在評估 function 中放置一個setTimeout但它似乎被簡單地忽略了

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

此代碼不要等待,只需在等待之前和等待之后編寫

你知道怎么做嗎?

你可以使用一點promise函數,

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

然后,只要你想延遲就調用它。

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

如果您必須使用 puppeteer,請使用內置的 waitForTimeout 函數。

await page.waitForTimeout(4000)

如果還想用page.evaluate,4秒后解決。 你沒有解決任何問題。

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

但我想你可以簡單地使用前兩個例子。

我一直在使用:

await page.waitForTimeout(3000);

其中 3000 是毫秒 這似乎對我有用。

您可以使用以下選項之一等待一秒鍾

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

或者,有許多包含內置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});

page.waitFor現在已被棄用。

現在建議page.waitForTimeout在繼續之前暫停腳本執行給定的毫秒數:

await page.waitForTimeout(1000)

試試這個功能。

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

使用它

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

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

其他答案已經顯示了如何睡覺,但現在page.waitForTimeout終於被棄用了,我想我會添加我想添加一段時間的答案:

不要睡覺,它會導致競爭條件破壞 Puppeteer 的事件驅動性質。 引入不必要的脆性:幾乎總是有更好的謂詞等待:

  • 您在等待選擇器出現嗎? 嘗試waitForSelector
  • 您是否在等待 XPath 出現? 試試waitForXPath
  • 你在等導航嗎? 試試waitForNavigation
  • 您在等待網絡響應嗎? 嘗試waitForNetworkIdlewaitForResponse
  • 您在等待彈出窗口嗎? 嘗試承諾.on("dialog", ...
  • 您是否正在等待一些任意謂詞,例如出現最少數量的子元素? 試試waitForFunction
  • 還有什么? 運行evaluate塊並添加您自己的代碼以等待 DOM 突變或使用setIntervalrequestAnimationFrame輪詢,並根據您的需要有效地重新實現waitForFunction

尤其是waitForFunction沒有得到充分利用,但它增加了waitForTimeout沒有的大量可靠性和精度。

如果所有其他方法都失敗並且您必須阻止腳本,您可以延遲,但我已經編寫了數百個 Puppeteer 腳本並且從未使用過任何睡眠變體,所以我非常相信這基本上是不必要的。

請參閱我在 Puppeteer Antipatterns 上的博客文章,了解為什么應該避免以任何形式睡覺,除非作為絕對的最后手段。

你的語法不完整。
試試這個...

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM