【问题标题】:Puppeteer throws "Protocol error (IO.read): Target closed." now and againPuppeteer 抛出“协议错误(IO.read):目标已关闭。”不时地
【发布时间】:2021-02-27 17:00:57
【问题描述】:

这让我发疯了...我有以下代码:

// Load a PUG template
const template = await loadTemplateRoute(pdfProps.layout);

// Generate HTML 
const html = template(pdfProps);

// requirement for puppeteer to work locally, if using locally
const executablePath = process.env.EXECUTABLE_PATH || await chromium.executablePath;
console.log('executable path', executablePath);

// These are needed to run on WSL
chromium.args.push(['--disable-gpu', '--single-process']);

console.log('1');

const browser = await puppeteer.launch({
  args: chromium.args,
  defaultViewport: chromium.defaultViewport,
  executablePath,
  headless: true,
  ignoreHTTPSErrors: true
});

console.log('2');

const page = await browser.newPage();

console.log('3');

// eslint-disable-next-line quote-props
await page.setContent(html, { 'waitUntil': 'networkidle2' });

console.log('4');

// here we can insert customizable features in the future using JSONB stored formats
const pdf = await page.pdf({
  format: 'A4',
  printBackground: true,
  margin: {
    top: '1cm',
    right: '1cm',
    bottom: '1cm',
    left: '1cm'
  }
});
await page.close();
  
console.log('5');
await browser.close();
console.log('6');
return pdf;

运行它会给我我想要的 PDF,但只有十次。其他时候我在console.log('4') 之后得到这个:

Protocol error (IO.read): Target closed.
    at .../node_modules/puppeteer-core/lib/Connection.js:183:56
    at new Promise (<anonymous>)
    at CDPSession.send (.../node_modules/puppeteer-core/lib/Connection.js:182:12)
    at Function.readProtocolStream (.../node_modules/puppeteer-core/lib/helper.js:254:37)
    at processTicksAndRejections (internal/process/task_queues.js:94:5)
    at Page.pdf (.../node_modules/puppeteer-core/lib/Page.js:1021:12)

或者在console.log('3')之后的其他时候(很少):

Navigation failed because browser has disconnected!
    at CDPSession.<anonymous> (.../node_modules/puppeteer-core/lib/LifecycleWatcher.js:46:107)
    at CDPSession.emit (events.js:223:5)
    at CDPSession.EventEmitter.emit (domain.js:475:20)
    at CDPSession._onClosed (.../node_modules/puppeteer-core/lib/Connection.js:215:10)
    at Connection._onClose (.../node_modules/puppeteer-core/lib/Connection.js:138:15)
    at WebSocket.<anonymous> (.../node_modules/puppeteer-core/lib/WebSocketTransport.js:48:22)
    at WebSocket.onClose (.../node_modules/puppeteer-core/node_modules/ws/lib/event-target.js:124:16)
    at WebSocket.emit (events.js:223:5)
    at WebSocket.EventEmitter.emit (domain.js:475:20)
    at WebSocket.emitClose (.../node_modules/puppeteer-core/node_modules/ws/lib/websocket.js:191:10)
    at Socket.socketOnClose (.../node_modules/puppeteer-core/node_modules/ws/lib/websocket.js:850:15)
    at Socket.emit (events.js:223:5)
    at Socket.EventEmitter.emit (domain.js:475:20)
    at TCP.<anonymous> (net.js:664:12)

我在 WSL Ubuntu 上运行它,但在 Mac 上运行它也会出错(但频率较低)。

如果我在两次尝试之间等待 5 分钟,似乎效果会更好,但列出进程 (ps -ef) 显示没有运行/挂起...

编辑:注销/node_modules/puppeteer-core/lib/Connection.js:182:56 中发生的事情:

send(); Page.printToPDF {
  transferMode: 'ReturnAsStream',
  landscape: false,
  displayHeaderFooter: false,
  headerTemplate: '',
  footerTemplate: '',
  printBackground: true,
  scale: 1,
  paperWidth: 8.27,
  paperHeight: 11.7,
  marginTop: 0.39375,
  marginBottom: 0.39375,
  marginLeft: 0.39375,
  marginRight: 0.39375,
  pageRanges: '',
  preferCSSPageSize: false
}
send(); IO.read { handle: '1' }
send(); IO.read { handle: '1' }

Page.printToPDF 工作正常,第一个 IO.read 也正常工作,而第二个 IO.read 抛出错误...

【问题讨论】:

    标签: node.js puppeteer


    【解决方案1】:

    在尝试了一堆东西后,我开始怀疑外部来源,因为它与简单的模板配合得很好。

    重新设计模板以不加载任何外部 CSS,而是将所有 CSS 放在 &lt;style&gt; 标记中并将所有图像“预解析”为 base64 (&lt;img src="data:image/png;MIIlkaa3498asm..." /&gt;) 它不再发生......

    很明显,有些资源让我很烦......

    【讨论】:

      猜你喜欢
      • 2019-01-08
      • 2019-07-02
      • 2018-04-27
      • 1970-01-01
      • 2018-11-11
      • 2018-11-10
      • 2022-10-25
      • 2021-09-23
      • 2022-12-22
      相关资源
      最近更新 更多