Is it possible to emulate mobile devices with Headless Chromium?

Yes, it is possible to emulate mobile devices with Headless Chromium. Chromium, the open-source version of Google Chrome, can be run in a headless mode, meaning it can operate without a graphical user interface. This can be useful for automated testing, web scraping, and other tasks where you do not need to actually view the browser.

To emulate a mobile device, you need to use the DevTools Protocol to instruct Chromium on how to simulate various device parameters such as screen size, pixel ratio, user agent, and touch events.

Here's how you can emulate a mobile device using Headless Chromium with Puppeteer (a Node library that provides a high-level API over the Chrome DevTools Protocol):

JavaScript/Node.js Example with Puppeteer

First, make sure you have Puppeteer installed. If not, install it using npm:

npm install puppeteer

Then, you can use the following code snippet to emulate a mobile device:

const puppeteer = require('puppeteer');

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

  // Define the device emulation settings
  const mobileDevice = puppeteer.devices['iPhone 6'];
  await page.emulate(mobileDevice);

  // Go to the page you want to scrape or test
  await page.goto('');

  // Take a screenshot as an example of an action
  await page.screenshot({ path: 'mobile_emulated.png' });

  await browser.close();

In the example above, puppeteer.devices is a predefined list of devices with their emulation settings. You can use any of them or define your own settings.

Python Example with Pyppeteer

Pyppeteer is a Python port of Puppeteer. You can use it to control Headless Chromium in a similar way.

First, install Pyppeteer:

pip install pyppeteer

Then, you can write a Python script to emulate a mobile device:

import asyncio
from pyppeteer import launch

async def main():
    browser = await launch(headless=True)
    page = await browser.newPage()

    # Define the device emulation settings
    await page.emulate({'viewport': {'width': 375, 'height': 667, 'isMobile': True, 'hasTouch': True}, 'userAgent': 'your-user-agent-string-here'})

    # Go to the page you want to scrape or test
    await page.goto('')

    # Take a screenshot as an example of an action
    await page.screenshot({'path': 'mobile_emulated.png'})

    await browser.close()


In this Python example, we manually defined device emulation settings inside page.emulate(). You can adjust the viewport size, isMobile, hasTouch, and userAgent properties to match the mobile device you want to emulate.

Remember that when scraping websites, it's important to respect the site's terms of service and robots.txt file, as well as to not overload the server with requests. Emulating a mobile device can be useful for testing responsive designs or scraping mobile-specific content, but it should be done responsibly and ethically.

Related Questions

Get Started Now

WebScraping.AI provides rotating proxies, Chromium rendering and built-in HTML parser for web scraping