Skip to main content
Version: 23.2.0

Page.emulateNetworkConditions() 方法

¥Page.emulateNetworkConditions() method

这不会影响 WebSocket 和 WebRTC PeerConnections(请参阅 https://crbug.com/563644)。要将页面设置为离线,你可以使用 Page.setOfflineMode()

¥This does not affect WebSockets and WebRTC PeerConnections (see https://crbug.com/563644). To set the page offline, you can use Page.setOfflineMode().

通过导入 PredefinedNetworkConditions 可以使用预定义网络条件列表。

¥A list of predefined network conditions can be used by importing PredefinedNetworkConditions.

签名

¥Signature

class Page {
abstract emulateNetworkConditions(
networkConditions: NetworkConditions | null
): Promise<void>;
}

参数

¥Parameters

范围

类型

描述

networkConditions

NetworkConditions | 无效的

传递 null 将禁用网络条件模拟。

Returns:

Promise<void>

示例

¥Example

import {PredefinedNetworkConditions} from 'puppeteer';
const slow3G = PredefinedNetworkConditions['Slow 3G'];

(async () => {
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.emulateNetworkConditions(slow3G);
await page.goto('https://www.google.com');
// other actions...
await browser.close();
})();