Page.emulate() 方法
¥Page.emulate() method
模拟给定设备的指标和用户代理。
¥Emulates a given device's metrics and user agent.
为了帮助模拟,Puppeteer 提供了可通过 KnownDevices 的已知设备列表。
¥To aid emulation, Puppeteer provides a list of known devices that can be via KnownDevices.
签名
¥Signature
class Page {
emulate(device: Device): Promise<void>;
}
参数
¥Parameters
范围 | 类型 | 描述 |
---|---|---|
device |
Returns:
Promise<void>
备注
¥Remarks
该方法是调用两个方法的快捷方式:Page.setUserAgent() 和 Page.setViewport()。
¥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
import {KnownDevices} from 'puppeteer';
const iPhone = KnownDevices['iPhone 15 Pro'];
(async () => {
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();
})();