所以我尝试了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:UnhandledPromiseRejection.这个错误可能是由于抛出一个没有catch块的异步函数内部,或者是由于拒绝了一个未用.catch()处理的承诺。(拒绝ID: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>
但它仍然会触发相同的错误。我做错了什么?
我想错误已经说了,它不能启动Chrome。所以我猜是你的chrome出了问题
您可能希望尝试一下https://github.com/puppeteer/puppeteer/issues/807中的一些建议