最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

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

IT培训 admin 6浏览 0评论

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

我在节点上运行[email protected]时遇到此错误:8-slim容器。

完整的错误:

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('');
          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 - .pub | apt-key add - \
    && sh -c 'echo "deb [arch=amd64] / 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容器中运行?我很满意任何有效的木偶操作版本。

从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('');
          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('');
          await page.screenshot({path: 'example.png'});
          await page.close();
          await browser.close();
        } catch(e){
                console.error({e});
        }
})();

完整的日志很长,可以在。

基本上,在几次重定向后,它降落在/?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: /
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: /
Labels:
Experimental: true
Insecure Registries:
 127.0.0.0/8
Live Restore Enabled: false
Product License: Community Engine
回答如下:

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

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

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

我在节点上运行[email protected]时遇到此错误:8-slim容器。

完整的错误:

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('');
          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 - .pub | apt-key add - \
    && sh -c 'echo "deb [arch=amd64] / 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容器中运行?我很满意任何有效的木偶操作版本。

从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('');
          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('');
          await page.screenshot({path: 'example.png'});
          await page.close();
          await browser.close();
        } catch(e){
                console.error({e});
        }
})();

完整的日志很长,可以在。

基本上,在几次重定向后,它降落在/?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: /
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: /
Labels:
Experimental: true
Insecure Registries:
 127.0.0.0/8
Live Restore Enabled: false
Product License: Community Engine
回答如下:

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

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

评论列表 (0)

  1. 暂无评论