PuppeteerNode 类
🌐 PuppeteerNode class
扩展主要的 Puppeteer 类,增加用于获取和下载浏览器的 Node 特定行为。
🌐 Extends the main Puppeteer class with Node specific behaviour for fetching and downloading browsers.
如果你在 Node 环境中使用 Puppeteer,当你运行 require('puppeteer')(或等效的 ES import)时,你将得到这个类。
🌐 If you're using Puppeteer in a Node environment, this is the class you'll get when you run require('puppeteer') (or the equivalent ES import).
语法
🌐 Signature
export declare class PuppeteerNode extends Puppeteer
扩展自: Puppeteer
附注
🌐 Remarks
最常用的方法是 launch,它用于启动并连接到新的浏览器实例。
🌐 The most common method to use is launch, which is used to launch and connect to a new browser instance.
请参阅 主 Puppeteer 类 以了解适用于所有环境的方法,例如 Puppeteer.connect()。
🌐 See the main Puppeteer class for methods common to all environments, such as Puppeteer.connect().
这个类的构造函数被标记为内部。第三方代码不应直接调用构造函数或创建继承 PuppeteerNode 类的子类。
🌐 The constructor for this class is marked as internal. Third-party code should not call the constructor directly or create subclasses that extend the PuppeteerNode class.
示例
🌐 Example
以下是使用 Puppeteer 驱动自动化的典型示例:
🌐 The following is a typical example of using Puppeteer to drive automation:
import puppeteer from 'puppeteer';
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto('https://www.google.com');
// other actions...
await browser.close();
一旦你创建了一个 page,你就可以访问一个大型的 API 来与页面交互、导航或查找该页面中的某些元素。`page` 文档列出了所有可用的方法。
🌐 Once you have created a page you have access to a large API to interact with the page, navigate, or find certain elements in that page. The `page` documentation lists all the available methods.
属性
🌐 Properties
属性 🌐 Property | 修饰符 🌐 Modifiers | 类型 🌐 Type | 描述 🌐 Description |
|---|---|---|---|
| defaultBrowser |
| 将默认启动的浏览器名称。对于 🌐 The name of the browser that will be launched by default. For | |
| lastLaunchedBrowser |
| 最后启动的浏览器的名称。 🌐 The name of the browser that was last launched. | |
| product |
| string | 已弃用: 不要使用此字段,因为它没有考虑不同类型的多个浏览器。请使用 defaultBrowser 或 lastLaunchedBrowser。 🌐 Do not use as this field as it does not take into account multiple browsers of different types. Use defaultBrowser or lastLaunchedBrowser. |
方法
🌐 Methods
方法 🌐 Method | 修饰符 🌐 Modifiers | 描述 🌐 Description |
|---|---|---|
| connect(options) | 此方法将 Puppeteer 附加到现有浏览器实例。 🌐 This method attaches Puppeteer to an existing browser instance. | |
| defaultArgs(options) | ||
| executablePath(channel) | 给定 ChromeReleaseChannel 的默认可执行路径。 🌐 The default executable path for a given ChromeReleaseChannel. | |
| executablePath(options) | 给定 LaunchOptions 的默认可执行路径。 🌐 The default executable path given LaunchOptions. | |
| executablePath() | 默认可执行路径。 🌐 The default executable path. | |
| launch(options) | 在指定时启动具有给定参数和选项的浏览器实例。 🌐 Launches a browser instance with given arguments and options when specified. 在使用 🌐 When using with 附注 Puppeteer 也可以用于控制 Chrome 浏览器,但它在默认下载的测试版 Chrome 上效果最佳。不能保证它能在其他版本上正常工作。如果更倾向于使用 Google Chrome(而不是测试版 Chrome),建议使用 Chrome Canary 或 Dev Channel 构建。有关 Chromium 与 Chrome 之间差异的说明,请参见 这篇文章。这篇文章 描述了 Linux 用户的一些差异。有关测试版 Chrome 的说明,请参见 这份文档。 🌐 Puppeteer can also be used to control the Chrome browser, but it works best with the version of Chrome for Testing downloaded by default. There is no guarantee it will work with any other version. If Google Chrome (rather than Chrome for Testing) is preferred, a Chrome Canary or Dev Channel build is suggested. See this article for a description of the differences between Chromium and Chrome. This article describes some differences for Linux users. See this doc for the description of Chrome for Testing. | |
| trimCache() | 删除由提供的 Puppeteer 配置识别的缓存目录中所有非当前的 Firefox 和 Chrome 二进制文件。当前的浏览器版本由 Puppeteer 解析 PUPPETEER_REVISIONS 确定,除非提供了 🌐 Removes all non-current Firefox and Chrome binaries in the cache directory identified by the provided Puppeteer configuration. The current browser version is determined by resolving PUPPETEER_REVISIONS from Puppeteer unless 附注 请注意,该方法不会检查主机上安装的使用相同缓存目录的任何其他 Puppeteer 版本是否需要非当前二进制文件。 🌐 Note that the method does not check if any other Puppeteer versions installed on the host that use the same cache directory require the non-current binaries. |