gpt4 book ai didi

How to fix issue that deprecated 'page.waitForTimeout' method usage(如何修复已弃用“page.waitForTimeout”方法用法问题)

转载 作者:bug小助手 更新时间:2023-10-24 20:50:11 32 4
gpt4 key购买 nike



I made a web scraping script using the NodeJS puppeteer package based on TypeScript.

我用基于类型脚本的NodeJS puppeteer包制作了一个Web抓取脚本。


I used await page.waitForTimeout(3000); method to delay the script running until page loading. This function works but it has the following issue:

我使用aWait page.waitForTimeout(3000);方法将脚本运行延迟到页面加载。此函数可以工作,但存在以下问题:


The signature '(milliseconds: number): Promise<void>' of 'page.waitForTimeout' is deprecated.ts(6387)

types.d.ts(5724, 8): The declaration was marked as deprecated here.

I want to use an alternative instead of using a deprecated function.

我想使用替代函数,而不是使用不推荐使用的函数。


更多回答

Would you mind providing the version of puppeteer you are using?

你介意提供你正在使用的木偶师的版本吗?

Hello @Wyck. Thank you for your comment. I am using v21.1.1 puppeteer.

您好@Wyck。谢谢你的评论。我使用的是v21.1.1木偶操纵者。

优秀答案推荐

The best solution is not to use it, because arbitrary sleeps are slow and unreliable. Prefer event-driven predicates like waitForSelector, waitForFunction, waitForResponse, etc. See this post for details.

最好的解决方案是不使用它,因为任意睡眠缓慢且不可靠。更喜欢事件驱动的谓词,如waitForSelector、waitForFunction、waitForResponse等。


That said, if you need to sleep to help debug code temporarily, or you're writing a quick and dirty hack script which doesn't need to be reliable, just use a plain Node wait:

也就是说,如果您需要暂时休眠以帮助调试代码,或者您正在编写不需要可靠的快速而肮脏的黑客脚本,只需使用简单的Node Wait:


import {setTimeout} from "node:timers/promises";

// ...
await setTimeout(3000);

Also possible is promisifying setTimeout yourself:

还有一种可能就是自己缩短setTimeout时间:




const sleep = ms => new Promise(res => setTimeout(res, ms));

(async () => {
console.log(new Date().getSeconds());
await sleep(3000);
console.log(new Date().getSeconds());
})();




更多回答

I fixed that issue using await new Promise(r => setTimeout(r, 3000)). Thank you.

我使用AWait new Promise(r=>setTimeout(r,3000))修复了这个问题。谢谢。

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com