我是 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的示例可以完美运行,因此我不认为需要使用在这里提到的修复措施。感谢您的帮助。
await
周围使用try
/catch
处理拒绝。尽管在这种情况下,你除了打印错误消息并使测试失败之外,没有太多可以做的事情。 - Bergipage.evaluate()
返回一个 promise,所以你应该使用await
等待它执行完毕。目前,你没有等待它完成,而是在page.evaluate()
结束之前立即执行了browser.close()
。 - Svenskunganka