I want to take a screenshot of a webpage, but no matter what I do puppeteer always crashes。
const puppeteer = require('puppeteer');
(async () => {
const browser = await puppeteer.launch({
headless: false
});
const page = await browser.newPage();
await page.goto('https://www.google.com/');
await page.screenshot({path: 'screenshot/example.png'});
await page.waitFor(5 * 1000);
await browser.close();
})();
I saved the above code as web.js, and executed node web.js in cmd. It crashed 2 seconds after the Chrome window appeared, and the page did not load。
(node:27064) UnhandledPromiseRejectionWarning: #
(Use node --trace-warnings ...
to show where the warning was created)
(node:27064) 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(). To terminate the node process on unhandled promise rejection, use the CLI flag --unhandled-rejections=strict
(see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:27064) [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.
try-catch
over this block. Just put it there and inside your catch, check what error is thrown. – Wiktor Zychla