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 |
|---|---|---|
|
添加一个新屏幕,返回已添加的屏幕信息对象。 🌐 Adds a new screen, returns the added screen information object. 附注 仅在无头模式下支持。 🌐 Only supported in headless mode. | ||
|
获取打开的浏览器上下文列表。 🌐 Gets a list of open browser contexts. 在新创建的浏览器中,这将返回一个BrowserContext的单个实例。 🌐 In a newly-created browser, this will return a single instance of BrowserContext. | ||
|
|
返回默认 BrowserContext 中的所有 cookie。 🌐 Returns all cookies in the default BrowserContext. 附注 [browser.defaultBrowserContext().cookies()] 的快捷方式。 🌐 Shortcut for browser.defaultBrowserContext().cookies(). | |
|
创建一个新的 浏览器上下文 。 🌐 Creates a new browser context. 这不会与其他浏览器上下文共享 cookies/缓存。 🌐 This won't share cookies/cache with other browser contexts. | ||
|
获取默认的 浏览器上下文 。 🌐 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(). | |
|
将 Puppeteer 与这个 浏览器 断开,但保持进程运行。 🌐 Disconnects Puppeteer from this browser, but leaves the process running. | ||
|
检索浏览器中安装的所有扩展的映射,其中键是扩展 ID,值是相应的 Extension 实例。 🌐 Retrieves a map of all extensions installed in the browser, where the keys are extension IDs and the values are the corresponding Extension instances. | ||
|
返回已安装渐进式网络应用(PWA)的操作系统集成状态,比如它的徽章数量和已注册的文件处理程序。 🌐 Returns the OS-integration state of an installed Progressive Web App (PWA), such as its badge count and registered file handlers. 附注 仅可通过管道连接使用。请参见 Browser.installPWA()。仅对当前已安装的应用有意义;查询未知的清单 ID 会被拒绝。 🌐 Only available over a pipe connection. See Browser.installPWA(). Meaningful only for an app that is currently installed; querying an unknown manifest id rejects. | ||
|
获取指定窗口的边界 bounds 。 🌐 Gets the specified window bounds. | ||
|
安装扩展并返回其 ID。 🌐 Installs an extension and returns the ID. | ||
|
安装一个渐进式网页应用(PWA)并返回它的清单ID。 🌐 Installs a Progressive Web App (PWA) and returns its manifest id. 附注 只有在通过管道连接连接到浏览器时才可用。在 puppeteer.launch 中设置 🌐 Only available when connected to the browser over a pipe connection. Set 返回的 manifest id 与 InstallPWAOptions.manifestId 对应,因此可以直接传递给 Browser.launchPWA()、Browser.getPWAState() 或 Browser.uninstallPWA()。 🌐 The returned manifest id echoes InstallPWAOptions.manifestId, so it can be passed directly to Browser.launchPWA(), Browser.getPWAState(), or Browser.uninstallPWA(). | ||
|
启动已安装的渐进式网页应用(PWA),并返回支持应用窗口的页面。 🌐 Launches an installed Progressive Web App (PWA) and resolves with the page backing the app window. 附注 仅在管道连接下可用。请参阅 Browser.installPWA()。 🌐 Only available over a pipe connection. See Browser.installPWA().
| ||
|
🌐 Creates a new page in the default browser context. | ||
|
🌐 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 | ||
|
获取关联的 ChildProcess。 🌐 Gets the associated ChildProcess. | ||
|
移除屏幕。 🌐 Removes a screen. 附注 仅支持无头模式。如果指定了主屏幕ID,则会失败。 🌐 Only supported in headless mode. Fails if the primary screen id is specified. | ||
|
获取 屏幕信息对象 的列表。 🌐 Gets a list of screen information objects. | ||
|
|
在默认的 BrowserContext 中设置 cookies。 🌐 Sets cookies in the default BrowserContext. 附注 [browser.defaultBrowserContext().setCookie()] 的快捷方式。 🌐 Shortcut for browser.defaultBrowserContext().setCookie(). | |
|
设置默认 BrowserContext 中特定来源的权限。 🌐 Sets the permission for a specific origin in the default BrowserContext. 附注 [browser.defaultBrowserContext().setPermission()] 的快捷方式。 🌐 Shortcut for browser.defaultBrowserContext().setPermission(). | ||
|
设置指定窗口的边界bounds。 🌐 Sets the specified window bounds. | ||
|
🌐 Gets the target associated with the default browser context). | ||
|
获取所有活动的 目标。 🌐 Gets all active targets. 在有多个浏览器上下文的情况下,这将返回所有浏览器上下文中的所有目标。 🌐 In case of multiple browser contexts, this returns all targets in all browser contexts. | ||
|
卸载扩展程序。 🌐 Uninstalls an extension. | ||
|
卸载之前安装的渐进式网页应用(PWA)。 🌐 Uninstalls a previously installed Progressive Web App (PWA). 附注 仅在管道连接下可用。请参阅 Browser.installPWA()。 🌐 Only available over a pipe connection. See Browser.installPWA(). | ||
|
获取此浏览器的原始用户代理。 🌐 Gets this browser's original user agent. Pages 可以使用 Page.setUserAgent() 覆盖用户代理。 | ||
|
获取表示此浏览器名称和版本的字符串。 🌐 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. | ||
|
等待直到出现与给定 🌐 Waits until a target matching the given 这将显示所有打开的浏览器上下文。 🌐 This will look all open browser contexts. | ||
|
获取用于连接此 浏览器 的 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 |