0
0

Puppeteerでpage.waitForTimeout' is deprecated.

Posted at

PuppeteerでwaitForTimeoutが非推奨(deprecated)になってました。

  • Puppeteer v21.5.2
  • Node.js v21.2.0

の環境です。

"node:timers/promises"を使うように

こちらにあるように.waitForTimeout()は公式のsetTimeout()を使いましょう的な感じでした。

const setTimeout = require("node:timers/promises").setTimeout;
//import {setTimeout} from "node:timers/promises";

// ...
await setTimeout(3000);

とすればOKです。

なるべく時間で待つのは使わない方がよい

Stack Over Flowであった内容はこんな感じで

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などのようなイベント駆動述語を好む。詳細については、この投稿を参照して下さい。
https://stackoverflow.com/questions/77078345/how-to-fix-issue-that-deprecated-page-waitfortimeout-method-usage

公式GitHubだとこんな感じ

一般的には、秒数を指定して待つのではなく、Frame.waitForSelector()、Frame.waitForXPath()、またはFrame.waitForFunction()を使って、欲しい条件を正確に待つことをお勧めします。
It's generally recommended to not wait for a number of seconds, but instead use Frame.waitForSelector(), Frame.waitForXPath() or Frame.waitForFunction() to wait for exactly the conditions you want.
https://github.com/puppeteer/puppeteer/blob/puppeteer-v21.5.2/docs/api/puppeteer.page.waitfortimeout.md#remarks

  • .waitForSelector()
  • .waitForXPath()
  • .waitForFunction()

などを使いましょう、そもそも秒数指定はやめてねって感じでした。

確かに何秒待ったかは関係なく欲しい状態になっているかが重要ですよね。

とはいえ難しい場合もあるのでその時はsetTimeout()使いましょう的な解釈かなと思いました。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0