puppeteer开启chromium,报UnhandledPromiseRejectionWarning?

汪大大坚
  • 1.1k
const puppeteer = require('puppeteer');

(async () => {
  const browser = await puppeteer.launch({
    devtools: true,
    ignoreHTTPSErrors: true
  });

  const page = await browser.newPage();
  page.setUserAgent('Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1');
  page.setViewport({
    width: 375,
    height: 667,
    isMobile: true
  });
  await page.goto('https://baidu.com', {
    waitUntil: 'networkidle2'
  });
  await browser.close();
})();

node以上代码会报错,去除page.setViewport中的isMobile参数后正常了,这是什么原因造成的?怎么解决呢?

chromium正常打开,链接没打开,报错

报错信息:

> node index

(node:13560) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): Error: net::ERR_ABORTED at https://baidu.com
(node:13560) [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.

再问个问题,打开chromium后,自动打开devtools,怎么让这个开发者工具自动设置成手机视图,就是下面这个模式:

clipboard.png

回复
阅读 3.4k
1 个回答

page.setViewport前加await

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
你知道吗?

宣传栏