木偶的铬打开错误并停止木偶脚本

时间:2019-08-23 17:40:46

标签: promise async-await chromium puppeteer unhandled-exception

我正在尝试在系统上设置人偶。我遵循了许多不同的教程和指南,但是我一直遇到这个问题。铬似乎无法正确打开。以下内容基于我遵循的其中一项指南中的示例测试。

我从铬得到的错误:

image of chromium

CLI上的错误消息:

(node:17168) UnhandledPromiseRejectionWarning: Error: Page crashed!
    at Page._onTargetCrashed (C:\Users\dgros\Documents\Git\test\pup\node_modules\puppeteer\lib\Page.js:215:24)
    at CDPSession.Page.client.on.event (C:\Users\dgros\Documents\Git\test\pup\node_modules\puppeteer\lib\Page.js:123:56)
    at CDPSession.emit (events.js:198:13)
    at CDPSession._onMessage (C:\Users\dgros\Documents\Git\test\pup\node_modules\puppeteer\lib\Connection.js:200:12)
    at Connection._onMessage (C:\Users\dgros\Documents\Git\test\pup\node_modules\puppeteer\lib\Connection.js:112:17)
    at WebSocketTransport._ws.addEventListener.event (C:\Users\dgros\Documents\Git\test\pup\node_modules\puppeteer\lib\WebSocketTransport.js:44:24)
    at WebSocket.onMessage (C:\Users\dgros\Documents\Git\test\pup\node_modules\ws\lib\event-target.js:120:16)
    at WebSocket.emit (events.js:198:13)
    at Receiver.receiverOnMessage (C:\Users\dgros\Documents\Git\test\pup\node_modules\ws\lib\websocket.js:789:20)
    at Receiver.emit (events.js:198:13)
(node:17168) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function wit
hout a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:17168) [DEP0018] DeprecationWarning: Unhandled promise 

我遵循的指南来自:https://www.youtube.com/watch?v=IvaJ5n5xFqU

我已经安装了node.js的10.16.0版和puppeteer的1.19.0版

我用以下方法安装了puppeteer:

npm i puppeteer

我使用以下命令从Git Bash运行的示例测试:“ node index.js”:

const puppeteer = require('puppeteer');

(async function main() {
    try {

        const browser = await puppeteer.launch({ headless: false, ignoreDefaultArgs: ['--disable-extensions'], devtools: true});
        const page = await browser.newPage();

        page.setUserAgent('Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36')

        await page.goto('https://experts.shopify.com');

        await page.waitForNavigation();

        console.log('its showing');

    } catch (e) {
        console.log('our error', e);
    }
})();

相同的错误产生于:

const puppeteer = require('puppeteer');

(async function main() {
    try {
        const browser = await puppeteer.launch({ headless: false, ignoreDefaultArgs: ['--disable-extensions'], devtools: true});
        const page = await browser.newPage();

    } catch (e) {
        console.log('our error', e);
    }
})();

当我省略带有“ browser.newPage()”的行时,铬是“ Aw,Snap!”。错误仍然存​​在,但是UnhandledPromiseRejectionWarning错误消息消失了。

预期:铬打开,导航到“ https://experts.shopify.com”,等待其加载,然后在Git Bash CLI中打印“显示”。

实际:铬打开时出现错误,并且错误消息输出到Git Bash CLI,没有其他反应。

为什么铬开口错误?如何让脚本运行并按预期方式运行?

1 个答案:

答案 0 :(得分:0)

您的问题似乎可能是由异步编排问题引起的(请参见issueissue)。

为了解决此问题,您可以尝试使用以下两种解决方案之一:

解决方案1 ​​

await Promise.all([
    page.goto('https://experts.shopify.com'),
    page.waitForNavigation()
]);

解决方案2

const waitForNavigation = page.waitForNavigation();
await page.goto('https://experts.shopify.com');
await waitForNavigation;

希望有帮助!