跳至主要內容
版本:23.11.1

Page.emulate() 方法

模擬指定裝置的指標和使用者代理程式。

為了協助模擬,Puppeteer 提供了一系列已知裝置,可透過 KnownDevices 來存取。

簽章

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

參數

參數

類型

描述

device

Device

回傳

Promise<void>

備註

此方法是呼叫兩個方法的捷徑:Page.setUserAgent()Page.setViewport()

此方法會調整頁面的大小。許多網站不希望手機變更大小,因此您應該在導覽至頁面之前進行模擬。

範例

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