【问题标题】:Puppeteer Error: Protocol error (Page.captureScreenshot): Target closedPuppeteer 错误:协议错误(Page.captureScreenshot):目标已关闭
【发布时间】:2019-07-02 21:13:59
【问题描述】:

我在 node:8-slim 容器上运行 puppeteer@1.12.2 时遇到此错误。

完整的错误:

Error: Protocol error (Page.captureScreenshot): Target closed.
    at Promise (/app/node_modules/puppeteer/lib/Connection.js:183:56)
    at new Promise (<anonymous>)
    at CDPSession.send (/app/node_modules/puppeteer/lib/Connection.js:182:12)
    at Page._screenshotTask (/app/node_modules/puppeteer/lib/Page.js:903:39)
    at <anonymous>
    at process._tickCallback (internal/process/next_tick.js:189:7)
  -- ASYNC --
    at Page.<anonymous> (/app/node_modules/puppeteer/lib/helper.js:108:27)
    at /app/test.js:9:15
    at <anonymous>
    at process._tickCallback (internal/process/next_tick.js:189:7)

js文件(灵感来自GoogleChrome/puppeteer/examples/screenshot.js):

const puppeteer = require('puppeteer');
(async() => {
         const browser = await puppeteer.launch({
                headless: true,
                args: ['--no-sandbox', '--disable-setuid-sandbox', '--disable-gpu', '--disable-dev-shm-usage']
         });
          const page = await browser.newPage();
          await page.goto('http://google.com');
          await page.screenshot({path: 'example.png'});
          await browser.close();
})();

Dockerfile(灵感来自troubleshooting.md#running-puppeteer-in-docker):


FROM node:8-slim

RUN apt-get update && apt-get install -yq libgconf-2-4

RUN apt-get update && apt-get install -y wget --no-install-recommends \
    && wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add - \
    && sh -c 'echo "deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google.list' \
    && apt-get update \
    && apt-get install -y google-chrome-unstable fonts-ipafont-gothic fonts-wqy-zenhei fonts-thai-tlwg fonts-kacst ttf-freefont \
      --no-install-recommends \
    && rm -rf /var/lib/apt/lists/* \
    && apt-get purge --auto-remove -y curl \
    && rm -rf /src/*.deb

WORKDIR /app
RUN chown node: /app

user node

COPY ./ ./
RUN npm i --unsafe-perm=true

CMD ["node", "test.js"]

相同的 test.js 在主机操作系统上制作高质量的屏幕截图,但在容器中失败。

是否有任何神奇的参数可以在 node-slim 容器中运行它?我对任何版本的 puppeteer 都很满意。

从 Dockerfile 安装的 google-chrome-unstable 版本是“73.0.3683.20 dev”。 npm 安装的 chrome 版本为“73.0.3679.0”。

更新:

我已尝试将await page.close() 添加为建议的Cody G.

const puppeteer = require('puppeteer');
(async() => {
         const browser = await puppeteer.launch({
                headless: true,
                args: ['--no-sandbox', '--disable-setuid-sandbox', '--disable-gpu', '--disable-dev-shm-usage']
         });
          const page = await browser.newPage();
          await page.goto('http://google.com');
          await page.screenshot({path: 'example.png'});
          await page.close();
          await browser.close();
})();

这并没有太大区别。错误仍然在第 9 行 await page.screenshot 抛出,所以我猜它没有到达添加的行。

更新 2:

我添加了这样的事件记录器:

const puppeteer = require('puppeteer');
(async() => {
        try{
         const browser = await puppeteer.launch({
                headless: true,
                args: ['--no-sandbox', '--disable-setuid-sandbox', '--disable-gpu', '--disable-dev-shm-usage']
         });
          const page = await browser.newPage();
          const eventHandler = e=>(...args)=>console.log({e,args});
          for(let e of [
                  'close',
                  'console',
                  'dialog',
                  'domcontentloaded',
                  'error',
                  'frameattached',
                  'framedetached',
                  'framenavigated',
                  'load',
                  'metrics',
                  'pageerror',
                  'popup',
                  'request',
                  'requestfailed',
                  'requestfinished',
                  'response',
                  'workercreated',
                  'workerdestroyed'
          ]) {
                page.on(e, eventHandler(e));
          };
          await page.goto('http://google.com');
          await page.screenshot({path: 'example.png'});
          await page.close();
          await browser.close();
        } catch(e){
                console.error({e});
        }
})();

完整的日志很长,可在https://gist.github.com/blex18/10ae4eed389ca818d72218baad3dfad2 获得。

基本上,经过几次重定向后,它到达https://www.google.com/?gws_rd=ssl,然后是资源和 xhr 请求。

更新 3:

package.json:

{
  "name": "pup",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "puppeteer": "^1.12.2"
  }
}

Docker 信息 1(不起作用):

Containers: 19
 Running: 3
 Paused: 0
 Stopped: 16
Images: 118
Server Version: 18.09.1
Storage Driver: aufs
 Root Dir: /var/lib/docker/aufs
 Backing Filesystem: extfs
 Dirs: 205
 Dirperm1 Supported: true
Logging Driver: json-file
Cgroup Driver: cgroupfs
Plugins:
 Volume: local
 Network: bridge host ipvlan macvlan null overlay
 Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog
Swarm: inactive
Runtimes: runc
Default Runtime: runc
Init Binary: docker-init
containerd version: 9754871865f7fe2f4e74d43e2fc7ccd237edcbce
runc version: 96ec2177ae841256168fcf76954f7177af9446eb
init version: fec3683
Security Options:
 apparmor
 seccomp
  Profile: default
Kernel Version: 4.15.0-45-generic
Operating System: Ubuntu 18.04.1 LTS
OSType: linux
Architecture: x86_64
CPUs: 8
Total Memory: 15.5GiB
Name: u4
ID: 3ZO4:OYYQ:K2X6:5QDB:ZNYO:FYNG:6YKT:HMDT:W3LX:UOY3:IEVU:3FCY
Docker Root Dir: /var/lib/docker
Debug Mode (client): false
Debug Mode (server): false
Username: alex
Registry: https://index.docker.io/v1/
Labels:
Experimental: true
Insecure Registries:
 127.0.0.0/8
Live Restore Enabled: false
Product License: Community Engine

WARNING: No swap limit support

Docker info 2(就像一个魅力)

Containers: 15
 Running: 6
 Paused: 0
 Stopped: 9
Images: 56
Server Version: 18.09.1
Storage Driver: overlay2
 Backing Filesystem: extfs
 Supports d_type: true
 Native Overlay Diff: true
Logging Driver: json-file
Cgroup Driver: cgroupfs
Plugins:
 Volume: local
 Network: bridge host ipvlan macvlan null overlay
 Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog
Swarm: inactive
Runtimes: runc
Default Runtime: runc
Init Binary: docker-init
containerd version: 9754871865f7fe2f4e74d43e2fc7ccd237edcbce
runc version: 96ec2177ae841256168fcf76954f7177af9446eb
init version: fec3683
Security Options:
 seccomp
  Profile: default
Kernel Version: 4.9.125-linuxkit
Operating System: Docker Desktop
OSType: linux
Architecture: x86_64
CPUs: 4
Total Memory: 1.952GiB
Name: docker-desktop
ID: E3X5:BZUG:4QYZ:Z6EA:TBT4:Y36Z:ZQ5Y:ACZS:QAS2:7I6M:LGN7:J3GH
Docker Root Dir: /var/lib/docker
Debug Mode (client): false
Debug Mode (server): true
 File Descriptors: 73
 Goroutines: 82
 System Time: 2019-02-13T10:04:59.482848Z
 EventsListeners: 2
HTTP Proxy: gateway.docker.internal:3128
HTTPS Proxy: gateway.docker.internal:3129
Registry: https://index.docker.io/v1/
Labels:
Experimental: true
Insecure Registries:
 127.0.0.0/8
Live Restore Enabled: false
Product License: Community Engine

【问题讨论】:

  • 如果将 await page.close() 放在 await browser.close() 之前会发生什么? (类似于)github.com/GoogleChrome/puppeteer/issues/843
  • @CodyG。它没有多大帮助。查看更新。
  • http://google.com 是使用的实际 URL 吗?
  • @SébastienDeprez,是的,代码 sn-p、Dockerfile 和错误消息都精确到一位。如果有帮助,我已经用一些日志更新了这个问题。我还尝试了其他 url,包括像 php.net 这样的非安全 URL,并得到了同样的错误。
  • 您对 docker 容器或托管它的服务器是否有任何 CPU/RAM 限制?

标签: node.js docker puppeteer


【解决方案1】:

我能够复制这个问题。

这是我的代码

browser = await puppeteer.launch({
      defaultViewport:{width:1920,height:6000},
      headless: true,
      executablePath: await chromium.executablePath,
      args: chromium.args,
      ignoreDefaultArgs: ["--hide-scrollbars"],
    });

let siteName = urlArray[urlIndex].split('://')[1];
      const page = await browser.newPage();
      await page.goto(urlArray[urlIndex], {
        waitUntil: ['domcontentloaded', 'networkidle0', 'networkidle2'],
      });
      
      const buffer = await page.screenshot({
          clip: { x: 0, y: 0, width:1920, height: 6000}
      });
      result = await page.title();

错误

START RequestId:51ec6b89-6c49-408d-95e1-f09492e1ecee 版本:$LATEST 2021-06-04T14:12:05.738Z 51ec6b89-6c49-408d-95e1-f09492e1ecee 错误错误:导航失败,因为浏览器已断开连接! 在 CDPSession.LifecycleWatcher._eventListeners.helper.addEventListener (/opt/nodejs/node_modules/puppeteer-core/lib/LifecycleWatcher.js:46:107) 在 CDPSession.emit (events.js:198:13)

错误:协议错误(Page.captureScreenshot):目标已关闭

解决方案 由于屏幕截图的高度和宽度,我遇到了问题。我的页面太长,所以我需要将屏幕截图的高度增加到 6000,这会导致浏览器断开连接或协议错误。此外,我使用相同的浏览器对象截取了导致上述问题的 4 个网站的屏幕截图。

所以,我在截屏之前重新初始化了浏览器对象:

for (let urlIndex = 0; urlIndex < urlArray.length; urlIndex++) {
    try {
      browser = await puppeteer.launch({
        defaultViewport:{width:1920,height:6000},
        headless: true,
        executablePath: await chromium.executablePath,
        args: chromium.args,
        ignoreDefaultArgs: ["--hide-scrollbars"],
      });

它解决了我的问题。

【讨论】:

    【解决方案2】:

    您的问题不可重现。它适用于我的 Centos 7,你的 Mac 环境,所以这似乎是 Docker 配置的问题,而不是代码的问题。可能的问题:

    1. 使用的 docker 存储驱动程序:aufs 可能是个问题,尤其是对于较旧的内核和容器中的写入/删除操作;如果可能,请尝试默认 overlay2
    2. 使用的安全配置文件:apparmorseccomp 已启用,您可以尝试使用 --security-opt seccomp=unconfined 运行并检查 dmesg,可能安全性阻止了某些东西

    【讨论】:

      【解决方案3】:

      我可以为您提供解决方案

          FROM node:8-slim
      
      # See https://crbug.com/795759
      RUN apt-get update && apt-get install -yq libgconf-2-4
      
      # Install latest chrome dev package and fonts to support major charsets (Chinese, Japanese, Arabic, Hebrew, Thai and a few others)
      # Note: this installs the necessary libs to make the bundled version of Chromium that Puppeteer
      # installs, work.
      RUN apt-get update && apt-get install -y wget --no-install-recommends \
          && wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add - \
          && sh -c 'echo "deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google.list' \
          && apt-get update \
          && apt-get install -y google-chrome-unstable fonts-ipafont-gothic fonts-wqy-zenhei fonts-thai-tlwg fonts-kacst ttf-freefont \
            --no-install-recommends \
          && rm -rf /var/lib/apt/lists/* \
          && apt-get purge --auto-remove -y curl \
          && rm -rf /src/*.deb
      
      # It's a good idea to use dumb-init to help prevent zombie chrome processes.
      ADD https://github.com/Yelp/dumb-init/releases/download/v1.2.0/dumb-init_1.2.0_amd64 /usr/local/bin/dumb-init
      RUN chmod +x /usr/local/bin/dumb-init
      
      # Uncomment to skip the chromium download when installing puppeteer. If you do,
      # you'll need to launch puppeteer with:
      #     browser.launch({executablePath: 'google-chrome-unstable'})
      # ENV PUPPETEER_SKIP_CHROMIUM_DOWNLOAD true
      
      # Install puppeteer so it's available in the container.
      RUN npm i puppeteer
      
      # Add user so we don't need --no-sandbox.
      RUN groupadd -r pptruser && useradd -r -g pptruser -G audio,video pptruser \
          && mkdir -p /home/pptruser/Downloads \
          && chown -R pptruser:pptruser /home/pptruser \
          && chown -R pptruser:pptruser /node_modules
      
      # Run everything after as non-privileged user.
      USER pptruser
      
      ENTRYPOINT ["dumb-init", "--"]
      CMD ["google-chrome-unstable"]
      

      这是迄今为止我为基于 RPM 的系统的 google-chrome-stable 找到的最佳镜像。

      http://orion.lcg.ufrj.br/RPMS/myrpms/google/

      【讨论】:

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