Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use puppeteer to turn Chromium on in mobile debug mode?

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
  });

So what should I do?

like image 692
pigjane Avatar asked Aug 06 '19 02:08

pigjane


People also ask

Does Puppeteer use Chromium?

By default, Puppeteer downloads and uses a specific version of Chromium so its API is guaranteed to work out of the box. To use Puppeteer with a different version of Chrome or Chromium, pass in the executable's path when creating a Browser instance: const browser = await puppeteer.

How do I run Chrome in Puppeteer?

Show activity on this post. I'd like to add, perhaps what you want is using the package chrome-launcher which takes care of running the chrome browser. You can then use puppeteer. connect() to connect the puppeteer-core library to the browser opened and instrument it.

Can Puppeteer run in browser?

Puppeteer runs headless by default, but can be configured to run full (non-headless) Chrome or Chromium.


2 Answers

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.

Code Sample

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);
  // ...
})();
like image 184
Thomas Dondorf Avatar answered Sep 21 '22 19:09

Thomas Dondorf


The isMobile property is a part of the defaultViewport object, it means that you should put isMobile property inside the defaultViewport object.

Like this:

defaultViewport: {
  width: 375,
  height: 667,
  isMobile: true,
}

Full:

const browser = await puppeteer.launch({
  devtools: true,
  ignoreHTTPSErrors: true,
  defaultViewport: {
    width: 375,
    height: 667,
    isMobile: true,
  }
});
like image 39
Yevhen Laichenkov Avatar answered Sep 20 '22 19:09

Yevhen Laichenkov