当前位置: 首页 > 知识库问答 >
问题:

puppeteer文档中的代码无法运行UnhandledPromiseerEjectionWarning

方绪
2023-03-14

所以我尝试了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>

但它仍然会触发相同的错误。我做错了什么?

共有1个答案

鱼宜
2023-03-14

我想错误已经说了,它不能启动Chrome。所以我猜是你的chrome出了问题

您可能希望尝试一下https://github.com/puppeteer/puppeteer/issues/807中的一些建议

 类似资料:
  • Puppeteer 是一个 Node 库,它提供了一个高级 API 来通过 DevTools 协议控制 Chromium 或 Chrome。

  • 问题内容: 我在oracle站点上找到了此代码,但是我无法使用Eclipse启动,这是说“无法启动”。我已经安装了JDK 8,但是它不起作用… 有人有解决办法吗?:p 链接到找到代码的网站:https : //docs.oracle.com/javafx/8/embed_swing/jfxpub- embed_swing.htm 问题答案: 在Java 8中,即使没有方法,您也可以直接启动子类。问

  • 我尝试在eclipse中运行下面的代码,发布在这里。 但它并没有被执行。它抛出以下错误: 注意:我还添加了来自lib/extjmetr安装文件的外部罐 代码:

  • 目前,我在VS代码集成终端上运行Python代码时遇到问题,尽管代码在任何其他编辑器或Powershell(Windows用户)上都能完美运行。 我目前正在使用Python的Anaconda版本(PATH上的Anaconda),但在昨晚对VS代码进行了一些更新后,错误阻止了我在集成终端上运行代码。我尝试重新安装Anaconda和VS代码,但问题仍然存在。 第1行字符:41 ... onda3/py

  • 我是owlapi的新手,我正尝试在debian上编写一个java代码示例,以加载我已经使用Protégé构建的本体。我正在使用“owlapi-osgiDistribution-4.0.2.jar”,但总是得到这个错误: 我试图使用maven进行依赖,但徒劳。谁能告诉我如何解决这个问题,谢谢。

  • 右下角有一个小窗口,显示: 但是当我在本地终端中使用命令时,它可以顺利运行。