如何使用puppeteer在移动调试模式下打开Chromium? [英] How to use puppeteer to turn Chromium on in mobile debug mode?

查看:985
本文介绍了如何使用puppeteer在移动调试模式下打开Chromium?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用puppeteer启动Chrome,并且在移动调试模式下,这意味着单击devtools中的切换设备工具栏按钮。

I want to start Chrome with puppeteer, and in mobile debug mode, It means to click the 'toggle device toolbar' button in devtools.

对不起,我没有足够的威望上传图片。

Sorry, I don't have enough prestige to upload pictures.

我尝试了以下代码,但没有用:

I tried the following code but it didn't work:

const browser = await puppeteer.launch({
    devtools: true,
    ignoreHTTPSErrors: true,
    isMobile:true //I thought it would be fine to set isMobile: true, but not
  });

那我该怎么办?

推荐答案

要完全模拟移动设备,还必须指定其他值,例如 width height deviceScaleFactor hasTouch ,也可能是用户代理,以使网站认为您的浏览器是移动设备设备。您可以手动设置它们(请参阅Yevhen的回答),也可以使用puppeteer通过 puppeteer.devices 并通过调用 page.emulate

To fully emulate a mobile device, you also have to specify other values like width, height, deviceScaleFactor, hasTouch and maybe also the user agent to make the website believe your browser is a mobile device. You can either set them manually (see the answer by Yevhen) or use one of the default device descriptors puppeteer provides via puppeteer.devices and apply them by calling page.emulate.

代码示例

const puppeteer = require('puppeteer');
const iPhone = puppeteer.devices['iPhone 6'];

(async () => {
  const browser = await puppeteer.launch();
  const page = await browser.newPage();
  await page.emulate(iPhone);
  await page.goto(url);
  // ...
})();

这篇关于如何使用puppeteer在移动调试模式下打开Chromium?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆