Browser 类
🌐 Browser class
浏览器 表示一个浏览器实例,该实例可以是:
- 通过 Puppeteer.connect() 连接或通过 PuppeteerNode.launch() 启动。
浏览器 触发 各种事件,这些事件在 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 |
| boolean | Puppeteer 是否已连接到此 浏览器。 🌐 Whether Puppeteer is connected to this browser. |
| debugInfo |
| (实验性) 从 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() | ||
返回默认 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. | |
从默认的 BrowserContext 中移除 cookies。 🌐 Removes cookies from the default BrowserContext. 附注 [browser.defaultBrowserContext().deleteCookie()] 的快捷方式。 🌐 Shortcut for browser.defaultBrowserContext().deleteCookie(). | ||
从默认的 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 中,只有在浏览器使用 🌐 Installs an extension and returns the ID. In Chrome, this is only available if the browser was created using | |
| isConnected() |
| Puppeteer 是否已连接到此 浏览器。 🌐 Whether Puppeteer is connected to this browser. 已弃用: 🌐 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. 附注 非可见的页面,例如 🌐 Non-visible pages, such as | |
| 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. | |
在默认的 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() | 🌐 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 中,只有当浏览器是使用 🌐 Uninstalls an extension. In Chrome, this is only available if the browser was created using | |
| userAgent() | 获取此浏览器的原始用户代理。 🌐 Gets this browser's original user agent. Pages 可以使用 Page.setUserAgent() 覆盖用户代理。 | |
| version() | 获取表示此浏览器名称和版本的字符串。 🌐 Gets a string representing this browser's name and version. 对于无头浏览器,这类似于 🌐 For headless browser, this is similar to Browser.version() 的格式可能会随着浏览器的未来版本而变化。 🌐 The format of Browser.version() might change with future releases of browsers. | |
| waitForTarget(predicate, options) | 等待直到出现与给定 🌐 Waits until a target matching the given 这将显示所有打开的浏览器上下文。 🌐 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(). 你可以从 🌐 You can find the debugger URL ( 有关更多信息,请参见 浏览器端点。 🌐 See browser endpoint for more information. 附注 格式始终是 🌐 The format is always |