puppeteer:console.log in evaluate触发错误“Unhandled promise rejection”

时间:2017-10-24 19:24:25

标签: javascript node.js chromium puppeteer browser-testing

In puppeteer , when i want to use console.log from evaluate , it triggers me an error

const puppeteer = require('puppeteer');

(async () => {
  const browser = await puppeteer.launch({headless: false});

  const page = await browser.newPage();
  page.on('console', msg => console.log('PAGE LOG:', ...msg.args));
  await page.goto('http://google.com',  {waitUntil: 'load'});
  await page.evaluate(async() => console.log('url is ${location.href}'));
  browser.close();
})();  
  

(node:70544)UnhandledPromiseRejectionWarning:未处理的承诺   rejection(拒绝id:1):TypeError:undefined不可迭代   (节点:70544)[DEP0018]弃用警告:未处理的承诺   拒绝拒绝。在未来,承诺拒绝   未处理将以非零退出终止Node.js进程   代码。

你知道怎么办吗?

1 个答案:

答案 0 :(得分:1)

msg.args值未定义,因此当您尝试使用扩展运算符(...msg.args)时,它会失败。

记录msg.args,或将其包装在空/未定义的检查