Skip to main content
Version: 24.38.0

Browser 类

🌐 Browser class

浏览器 表示一个浏览器实例,该实例可以是:

浏览器 触发 各种事件,这些事件在 BrowserEvent 枚举中有文档记录。

语法

🌐 Signature

export declare abstract class Browser extends EventEmitter<BrowserEvents>

Extends: EventEmitter<BrowserEvents>

附注

🌐 Remarks

这个类的构造函数被标记为内部。第三方代码不应直接调用构造函数或创建继承 Browser 类的子类。

🌐 The constructor for this class is marked as internal. Third-party code should not call the constructor directly or create subclasses that extend the Browser class.

示例 1

🌐 Example 1

使用 浏览器 创建 页面:

🌐 Using a Browser to create a Page:

import puppeteer from 'puppeteer';

const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto('https://example.com');
await browser.close();

示例 2

🌐 Example 2

断开与重新连接到 浏览器:

🌐 Disconnecting from and reconnecting to a Browser:

import puppeteer from 'puppeteer';

const browser = await puppeteer.launch();
// Store the endpoint to be able to reconnect to the browser.
const browserWSEndpoint = browser.wsEndpoint();
// Disconnect puppeteer from the browser.
await browser.disconnect();

// Use the endpoint to reestablish a connection
const browser2 = await puppeteer.connect({browserWSEndpoint});
// Close the browser.
await browser2.close();

属性

🌐 Properties

属性

🌐 Property

修饰符

🌐 Modifiers

类型

🌐 Type

描述

🌐 Description

connected

readonly

boolean

Puppeteer 是否已连接到此 浏览器

🌐 Whether Puppeteer is connected to this browser.

debugInfo

readonly

调试信息

(实验性) 从 Puppeteer 获取调试信息。

附注

目前,包括待处理的协议调用。将来,我们可能会添加更多信息。

🌐 Currently, includes pending protocol calls. In the future, we might add more info.

方法

🌐 Methods

方法

🌐 Method

修饰符

🌐 Modifiers

描述

🌐 Description

addScreen(params)

添加一个新屏幕,返回已添加的屏幕信息对象

🌐 Adds a new screen, returns the added screen information object.

附注

仅在无头模式下支持。

🌐 Only supported in headless mode.

browserContexts()

获取打开的浏览器上下文列表。

🌐 Gets a list of open browser contexts.

在新创建的浏览器中,这将返回一个BrowserContext的单个实例。

🌐 In a newly-created browser, this will return a single instance of BrowserContext.

close()

关闭此 浏览器 以及所有相关的 页面

🌐 Closes this browser and all associated pages.

cookies()

返回默认 BrowserContext 中的所有 cookie。

🌐 Returns all cookies in the default BrowserContext.

附注

[browser.defaultBrowserContext().cookies()] 的快捷方式。

🌐 Shortcut for browser.defaultBrowserContext().cookies().

createBrowserContext(options)

创建一个新的 浏览器上下文

🌐 Creates a new browser context.

这不会与其他浏览器上下文共享 cookies/缓存。

🌐 This won't share cookies/cache with other browser contexts.

defaultBrowserContext()

获取默认的 浏览器上下文

🌐 Gets the default browser context.

附注

默认的浏览器上下文无法关闭。

🌐 The default browser context cannot be closed.

deleteCookie(cookies)

从默认的 BrowserContext 中移除 cookies。

🌐 Removes cookies from the default BrowserContext.

附注

[browser.defaultBrowserContext().deleteCookie()] 的快捷方式。

🌐 Shortcut for browser.defaultBrowserContext().deleteCookie().

deleteMatchingCookies(filters)

从默认的 BrowserContext 中删除与提供的过滤器匹配的 cookie。

🌐 Deletes cookies matching the provided filters from the default BrowserContext.

附注

[browser.defaultBrowserContext().deleteMatchingCookies()] 的快捷方式。

🌐 Shortcut for browser.defaultBrowserContext().deleteMatchingCookies().

disconnect()

将 Puppeteer 与这个 浏览器 断开,但保持进程运行。

🌐 Disconnects Puppeteer from this browser, but leaves the process running.

getWindowBounds(windowId)

获取指定窗口的边界 bounds

🌐 Gets the specified window bounds.

installExtension(path)

安装一个扩展并返回其 ID。在 Chrome 中,只有在浏览器使用 pipe: true 创建并设置了 --enable-unsafe-extension-debugging 标志时,此功能才可用。

🌐 Installs an extension and returns the ID. In Chrome, this is only available if the browser was created using pipe: true and the --enable-unsafe-extension-debugging flag is set.

isConnected()

deprecated

Puppeteer 是否已连接到此 浏览器

🌐 Whether Puppeteer is connected to this browser.

已弃用:

使用 Browser.connected

🌐 Use Browser.connected.

newPage(options)

默认浏览器上下文中创建一个新的页面

🌐 Creates a new page in the default browser context.

pages(includeAll)

获取此 浏览器 内所有打开的 页面 列表。

🌐 Gets a list of all open pages inside this Browser.

如果有多个浏览器上下文,则此方法会返回所有浏览器上下文中的所有页面

🌐 If there are multiple browser contexts, this returns all pages in all browser contexts.

附注

非可见的页面,例如 "background_page",将不会在此列出。你可以使用Target.page()找到它们。

🌐 Non-visible pages, such as "background_page", will not be listed here. You can find them using Target.page().

process()

获取关联的 ChildProcess

🌐 Gets the associated ChildProcess.

removeScreen(screenId)

移除屏幕。

🌐 Removes a screen.

附注

仅支持无头模式。如果指定了主屏幕ID,则会失败。

🌐 Only supported in headless mode. Fails if the primary screen id is specified.

screens()

获取 屏幕信息对象 的列表。

🌐 Gets a list of screen information objects.

setCookie(cookies)

在默认的 BrowserContext 中设置 cookies。

🌐 Sets cookies in the default BrowserContext.

附注

[browser.defaultBrowserContext().setCookie()] 的快捷方式。

🌐 Shortcut for browser.defaultBrowserContext().setCookie().

setPermission(origin, permissions)

设置默认 BrowserContext 中特定来源的权限。

🌐 Sets the permission for a specific origin in the default BrowserContext.

附注

[browser.defaultBrowserContext().setPermission()] 的快捷方式。

🌐 Shortcut for browser.defaultBrowserContext().setPermission().

setWindowBounds(windowId, windowBounds)

设置指定窗口的边界bounds

🌐 Sets the specified window bounds.

target()

获取与默认浏览器上下文关联的target

🌐 Gets the target associated with the default browser context).

targets()

获取所有活动的 目标

🌐 Gets all active targets.

在有多个浏览器上下文的情况下,这将返回所有浏览器上下文中的所有目标

🌐 In case of multiple browser contexts, this returns all targets in all browser contexts.

uninstallExtension(id)

卸载扩展程序。在 Chrome 中,只有当浏览器是使用 pipe: true 创建并且设置了 --enable-unsafe-extension-debugging 标志时才可用。

🌐 Uninstalls an extension. In Chrome, this is only available if the browser was created using pipe: true and the --enable-unsafe-extension-debugging flag is set.

userAgent()

获取此浏览器的原始用户代理。

🌐 Gets this browser's original user agent.

Pages 可以使用 Page.setUserAgent() 覆盖用户代理。

version()

获取表示此浏览器名称和版本的字符串。

🌐 Gets a string representing this browser's name and version.

对于无头浏览器,这类似于 "HeadlessChrome/61.0.3153.0"。对于非无头或新无头,这类似于 "Chrome/61.0.3153.0"。对于 Firefox,这类似于 "Firefox/116.0a1"

🌐 For headless browser, this is similar to "HeadlessChrome/61.0.3153.0". For non-headless or new-headless, this is similar to "Chrome/61.0.3153.0". For Firefox, it is similar to "Firefox/116.0a1".

Browser.version() 的格式可能会随着浏览器的未来版本而变化。

🌐 The format of Browser.version() might change with future releases of browsers.

waitForTarget(predicate, options)

等待直到出现与给定 predicate 匹配的 目标,然后返回它。

🌐 Waits until a target matching the given predicate appears and returns it.

这将显示所有打开的浏览器上下文

🌐 This will look all open browser contexts.

wsEndpoint()

获取用于连接此 浏览器 的 WebSocket URL。

🌐 Gets the WebSocket URL to connect to this browser.

这通常与 Puppeteer.connect() 一起使用。

🌐 This is usually used with Puppeteer.connect().

你可以从 http://HOST:PORT/json/version 找到调试器 URL (webSocketDebuggerUrl)。

🌐 You can find the debugger URL (webSocketDebuggerUrl) from http://HOST:PORT/json/version.

有关更多信息,请参见 浏览器端点

🌐 See browser endpoint for more information.

附注

格式始终是 ws://HOST:PORT/devtools/browser/<id>

🌐 The format is always ws://HOST:PORT/devtools/browser/<id>.