【发布时间】:2020-01-13 09:09:54
【问题描述】:
所以我尝试了 puppeteer 文档https://pptr.dev/ 上的示例代码,但它不起作用。 我通过 npm 安装了 puppeteer 3.0.0。
const puppeteer = require('puppeteer');
(async () => {
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto('https://example.com');
await page.screenshot({path: 'example.png'});
await browser.close();
})();
它触发:UnhandledPromiseRejectionWarning:未处理的承诺拒绝。此错误源于在没有 catch 块的情况下抛出异步函数内部,或拒绝未使用 .catch() 处理的承诺。 (拒绝编号:1)
所以我尝试了这段代码:
'use strict';
const puppeteer = require('puppeteer');
(async () => {
const browser = await puppeteer.launch();
const page = await browser.newPage();
try {
await page.goto('https://example.com');
await page.screenshot({path: 'example.png'});
} catch (err) {
console.error(err.message);
} finally {
await browser.close();
}
})();
编辑:完全错误
(node:12856) UnhandledPromiseRejectionWarning: Error: Failed to launch chrome!
TROUBLESHOOTING: https://github.com/GoogleChrome/puppeteer/blob/master/docs/troubleshooting.md
at onClose (C:\Users\tim\Desktop\te\node_modules\puppeteer\lib\Launcher.js:348:14)
at ChildProcess.helper.addEventListener (C:\Users\tim\Desktop\te\node_modules\puppeteer\lib\Launcher.js:338:60)
at ChildProcess.emit (events.js:194:15)
at Process.ChildProcess._handle.onexit (internal/child_process.js:248:12)
(node:12856) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:12856) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a
non-zero exit code.PS C:\Users\tim\Desktop\te>
但它仍然会触发相同的错误。我做错了什么?
【问题讨论】:
-
你能分享 UnhandledPromiseRejectionWarning 的堆栈跟踪吗?
-
@hardkoded 是你需要的吗?请参阅编辑
-
您是否按照错误上发布的故障排除进行操作?
-
@hardkoded 是的。它没有帮助。
标签: javascript npm puppeteer