Skip to main content
Version: 23.8.0

Page.setViewport() 方法

¥Page.setViewport() method

page.setViewport 将调整页面大小。许多网站不希望手机改变大小,因此你应该在导航到页面之前设置视口。

¥page.setViewport will resize the page. A lot of websites don't expect phones to change size, so you should set the viewport before navigating to the page.

在单个浏览器中有多个页面的情况下,每个页面可以有自己的视口大小。将视口设置为 null 会将视口重置为其默认值。

¥In the case of multiple pages in a single browser, each page can have its own viewport size. Setting the viewport to null resets the viewport to its default value.

签名

¥Signature

class Page {
abstract setViewport(viewport: Viewport | null): Promise<void>;
}

参数

¥Parameters

范围

类型

描述

viewport

视口 | 无效的

Returns:

Promise<void>

备注

¥Remarks

注意:在某些情况下,设置视口将重新加载页面以设置 isMobile 或 hasTouch 属性。

¥NOTE: in certain cases, setting viewport will reload the page in order to set the isMobile or hasTouch properties.

示例

¥Example

const page = await browser.newPage();
await page.setViewport({
width: 640,
height: 480,
deviceScaleFactor: 1,
});
await page.goto('https://example.com');