最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - UnhandledPromiseRejectionWarning when running Puppeteer? - Stack Overflow

programmeradmin0浏览0评论

Why do I get the following warnings, and how can I get rid of them?

Warnings:

(node:26771) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): Error: Protocol error (Runtime.callFunctionOn): Target closed.

(node:26771) [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-zeroexit code.

Code:

const puppeteer = require("puppeteer");

(async () => {
    const browser = await puppeteer.launch();
    const page = await browser.newPage();
    await page.goto(";, { waitUntil: "domcontentloaded" });
    var output = page.evaluate(() => {
        return;
    });
    await browser.close();
})();

Environment:

  • macOS High Sierra
  • Node v8.5.0
  • Puppeteer: 1.9.0

Why do I get the following warnings, and how can I get rid of them?

Warnings:

(node:26771) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): Error: Protocol error (Runtime.callFunctionOn): Target closed.

(node:26771) [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-zeroexit code.

Code:

const puppeteer = require("puppeteer");

(async () => {
    const browser = await puppeteer.launch();
    const page = await browser.newPage();
    await page.goto("https://dn.se", { waitUntil: "domcontentloaded" });
    var output = page.evaluate(() => {
        return;
    });
    await browser.close();
})();

Environment:

  • macOS High Sierra
  • Node v8.5.0
  • Puppeteer: 1.9.0
Share Improve this question edited Jun 20, 2020 at 9:12 CommunityBot 11 silver badge asked Oct 13, 2018 at 22:28 user1283776user1283776 21.8k57 gold badges154 silver badges292 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 10

You need to await page.evaluate(), as it returns a promise:

var output = await page.evaluate(() => {
  return;
});

Make sure you are using process.on('unhandledRejection') to listen for unhandled promise rejections and to gracefully close the browser if such an event should occur:

process.on('unhandledRejection', (reason, p) => {
  console.error('Unhandled Rejection at: Promise', p, 'reason:', reason);
  browser.close();
});

Your final code should end up looking like this:

'use strict';

const puppeteer = require('puppeteer');

(async () => {
  const browser = await puppeteer.launch();
  const page = await browser.newPage();

  process.on('unhandledRejection', (reason, p) => {
    console.error('Unhandled Rejection at: Promise', p, 'reason:', reason);
    browser.close();
  });

  await page.goto('https://dn.se', {
    waitUntil: 'domcontentloaded',
  });

  var output = await page.evaluate(() => {
    return;
  });

  await browser.close();
})();
发布评论

评论列表(0)

  1. 暂无评论