6

我是 Puppeteer 和 Node 的新手,尝试执行以下代码时出现此错误:

'use strict';

const puppeteer = require('puppeteer');
const evalLib = require('./oaa_a11y_evaluation.js');

function evaluateRules() {
  var ruleset = OpenAjax.a11y.RulesetManager.getRuleset("ARIA_STRICT");
  var evaluator_factory = OpenAjax.a11y.EvaluatorFactory.newInstance();
  evaluator_factory.setParameter('ruleset', ruleset);
  evaluator_factory.setFeature('eventProcessing', 'fae-util');
  evaluator_factory.setFeature('groups', 7);
  var evaluator = evaluator_factory.newEvaluator();
  var evaluation = evaluator.evaluate(window.document, document.title, document.location.href);
  // var out = evaluation.toJSON(true);
  return;
}

(async() => {
  const browser = await puppeteer.launch();
  var page = await browser.newPage();
  await page.goto('http://mihirkumar.com/', {waitUntil: 'load'});
  page.evaluate(evaluateRules);
  await browser.close();
})();

以下是详细的错误消息:

(node:27876) UnhandledPromiseRejectionWarning: Error: Protocol error (Runtime.callFunctionOn): Target closed.
at Promise (C:\Users\Mihir\fae\node_modules\puppeteer\lib\Connection.js:200:56)
at new Promise (<anonymous>)
at CDPSession.send (C:\Users\Mihir\fae\node_modules\puppeteer\lib\Connection.js:199:12)
at ExecutionContext.evaluateHandle (C:\Users\Mihir\fae\node_modules\puppeteer\lib\ExecutionContext.js:79:75)
at ExecutionContext.evaluate (C:\Users\Mihir\fae\node_modules\puppeteer\lib\ExecutionContext.js:46:31)
at Frame.evaluate (C:\Users\Mihir\fae\node_modules\puppeteer\lib\FrameManager.js:326:20)
at <anonymous>
at process._tickCallback (internal/process/next_tick.js:188:7)
(node:27876) 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:27876) [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.

https://github.com/GoogleChromeLabs/puppeteer-examples中的其他示例运行得非常好,所以我认为我不需要使用此处提到的修复程序。谢谢您的帮助。

4

3 回答 3

5

正如 Sven 在评论中提到的,在关闭浏览器之前,您应该await在每个page返回 promise 的函数(几乎是所有函数)上使用。

尝试这个:

'use strict';

const puppeteer = require('puppeteer');
const evalLib = require('./oaa_a11y_evaluation.js');

async function evaluateRules() {
  var ruleset = OpenAjax.a11y.RulesetManager.getRuleset("ARIA_STRICT");
  var evaluator_factory = OpenAjax.a11y.EvaluatorFactory.newInstance();
  evaluator_factory.setParameter('ruleset', ruleset);
  evaluator_factory.setFeature('eventProcessing', 'fae-util');
  evaluator_factory.setFeature('groups', 7);
  var evaluator = evaluator_factory.newEvaluator();
  var evaluation = evaluator.evaluate(window.document, document.title, document.location.href);
  // var out = evaluation.toJSON(true);
  return;
}

(async() => {
  const browser = await puppeteer.launch();
  var page = await browser.newPage();
  await page.goto('http://mihirkumar.com/', {waitUntil: 'load'});
  await page.evaluate(evaluateRules); // <-- await here
  await browser.close();
})();

于 2018-06-27T21:32:37.573 回答
1

感谢您的回复。使函数异步绝对是一个更好的改变。我的问题是我没有将 OpenAjax javascript 库注入到加载的页面中。一旦我使用 puppeteer 的 page.addScriptTag 函数做到这一点,事情就很好了。

于 2018-06-28T00:20:38.720 回答
0

如果你正在使用玩笑,只需添加“exitOnPageError:false”

module.exports = {
  server: {
    command: "PORT=3005 BROWSER=none react-scripts start",
    port: 3005,
    launchTimeout: 30000,
  },
  launch: {
    headless: process.env.HEADLESS !== "false",
    devtools: true,
  },
  exitOnPageError: false
};
于 2021-12-07T21:26:00.810 回答