Back to Puppeteer

Page.emulate() method

website/versioned_docs/version-24.40.0/api/puppeteer.page.emulate.md

19.2.21.2 KB
Original Source

Page.emulate() method

Emulates a given device's metrics and user agent.

To aid emulation, Puppeteer provides a list of known devices that can be via KnownDevices.

Signature

typescript
class Page {
  emulate(device: Device): Promise<void>;
}

Parameters

<table><thead><tr><th>

Parameter

</th><th>

Type

</th><th>

Description

</th></tr></thead> <tbody><tr><td>

device

</td><td>

Device

</td><td> </td></tr> </tbody></table>

Returns:

Promise<void>

Remarks

This method is a shortcut for calling two methods: Page.setUserAgent() and Page.setViewport().

This method will resize the page. A lot of websites don't expect phones to change size, so you should emulate before navigating to the page.

Example

ts
import {KnownDevices} from 'puppeteer';
const iPhone = KnownDevices['iPhone 15 Pro'];

const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.emulate(iPhone);
await page.goto('https://www.google.com');
// other actions...
await browser.close();