Puppeteer - 协议错误(Page.captureScreenshot):当我尝试截屏时,目标已关闭

时间:2017-11-14 18:54:11

标签: javascript node.js typescript puppeteer

我正在运行此示例代码:

async takeScreenshot() {  
        console.log('trying take Screenshot [...]');
        let image = this.page.screenshot({fullPage: true, type: 'jpeg'});
        timeout(image, 30000)
        .then((thing) => console.log("screenshot successfull!"))
        .catch((err) => {
            if(err instanceof TimeoutError) {
                console.log(err);
                image = null;
            }
        })

        console.log('Completed!');
        return image;
    }

我收到以下错误输出:

  

0 | era-moni |错误:协议错误(Page.captureScreenshot):目标已关闭。   0 | era-moni |在Session._onClosed(/home/ubuntu/project/ra-monitors/era-monitor-browser/node_modules/puppeteer/lib/Connection.js:209:23)   0 | era-moni |在Connection._onClose(/home/ubuntu/project/ra-monitors/era-monitor-browser/node_modules/puppeteer/lib/Connection.js:116:15)   0 | era-moni |在emitTwo(events.js:106:13)   0 | era-moni |在WebSocket.emit(events.js:194:7)   0 | era-moni |在WebSocket.emitClose(/home/ubuntu/project/ra-monitors/era-monitor-browser/node_modules/ws/lib/WebSocket.js:213:10)   0 | era-moni |在_receiver.cleanup(/home/ubuntu/project/ra-monitors/era-monitor-browser/node_modules/ws/lib/WebSocket.js:195:41)   0 | era-moni |在Receiver.cleanup(/home/ubuntu/project/ra-monitors/era-monitor-browser/node_modules/ws/lib/Receiver.js:520:15)   0 | era-moni |在WebSocket.finalize(/home/ubuntu/project/ra-monitors/era-monitor-browser/node_modules/ws/lib/WebSocket.js:195:22)   0 | era-moni |在emitNone(events.js:91:20)   0 | era-moni |在Socket.emit(events.js:188:7)

1 个答案:

答案 0 :(得分:0)

您需要在await之前使用page.screenshot()运算符,因为该函数是异步的:

let image = await this.page.screenshot({
  fullPage: true,
  type: 'jpeg',
});