2

我遇到了以下问题。

这是错误消息:

 Error: Navigation failed because browser has disconnected!
    at /Users/me/myproject/node_modules/puppeteer/lib/cjs/puppeteer/common/LifecycleWatcher.js:51:147
    at /Users/me/myproject/node_modules/puppeteer/lib/cjs/vendor/mitt/src/index.js:47:62
    at Array.map (<anonymous>)
    at Object.emit (/Users/me/myproject/node_modules/puppeteer/lib/cjs/vendor/mitt/src/index.js:47:43)
    at CDPSession.emit (/Users/me/myproject/node_modules/puppeteer/lib/cjs/puppeteer/common/EventEmitter.js:72:22)
    at CDPSession._onClosed (/Users/me/myproject/node_modules/puppeteer/lib/cjs/puppeteer/common/Connection.js:247:14)
    at Connection._onMessage (/Users/me/myproject/node_modules/puppeteer/lib/cjs/puppeteer/common/Connection.js:94:25)
    at WebSocket.<anonymous> (/Users/me/myproject/node_modules/puppeteer/lib/cjs/puppeteer/common/WebSocketTransport.js:13:32)
    at WebSocket.onMessage (/Users/me/myproject/node_modules/ws/lib/event-target.js:125:16)
    at WebSocket.emit (events.js:315:20)

这是我正在阅读的文档:

timeout <number> Specify a timeout for all tasks. Defaults to 30000 (30 seconds).
[src](https://github.com/thomasdondorf/puppeteer-cluster#clusterlaunchoptions)

我的每项任务都是约 34 秒,所以大多数时候它会失败(30 秒后超时),但偶尔它会成功。

其他 GitHub/Stack Overflow 的答案也提到了await代码中的缺失也可能导致相同的错误消息。

4

1 回答 1

1

这是我的解决方案,

前:

const cluster = await Cluster.launch({
    concurrency: Cluster.CONCURRENCY_PAGE,
    maxConcurrency: 2,
    puppeteerOptions: { headless: false }
  });

后:

const cluster = await Cluster.launch({
    concurrency: Cluster.CONCURRENCY_PAGE,
    maxConcurrency: 2,
    timeout: 150 * 1000, // 2.5 min timeout per task
    puppeteerOptions: { headless: false }
  });

把它留在这里供参考,以防其他人有这个问题。

于 2020-08-29T08:17:04.930 回答