WebDriver BiDi 支持
¥WebDriver BiDi support
WebDriver 双向 是一种新的跨浏览器自动化协议,目前正在开发中,旨在结合 WebDriver“经典”和 CDP 的优点。WebDriver BiDi 支持双向通信,默认情况下速度很快,并且配备了底层控制。
¥WebDriver BiDi is a new cross-browser automation protocol currently under development, aiming to combine the best of both WebDriver “Classic” and CDP. WebDriver BiDi enables bi-directional communication, making it fast by default, and it comes packed with low-level control.
使用 Chrome 和 Firefox 实现自动化
¥Automate with Chrome and Firefox
Puppeteer 支持 Chrome 和 Firefox 的 WebDriver BiDi 自动化。使用 Puppeteer 启动 Firefox 时,默认情况下启用 WebDriver BiDi 协议。启动 Chrome 时,默认情况下仍使用 CDP,因为 WebDriver BiDi 尚未支持所有 CDP 功能。如果某个 Puppeteer 功能尚不支持 WebDriver BiDi,则会引发 UnsupportedOperation
错误。另请参阅以下列表,了解 WebDriver BiDi 支持哪些功能。
¥Puppeteer supports WebDriver BiDi automation with Chrome and Firefox.
When launching Firefox with Puppeteer, the WebDriver BiDi Protocol is
enabled by default. When launching Chrome, CDP is still used by default
since not all CDP features are supported by WebDriver BiDi yet. If a
certain Puppeteer feature is not supported over WebDriver BiDi yet,
UnsupportedOperation
error is thrown. Also see the lists below on what is supported with
WebDriver BiDi.
开始使用
¥Get started
下面是使用 WebDriver BiDi 启动 Firefox 或 Chrome 的示例:
¥Below is an example of launching Firefox or Chrome with WebDriver BiDi:
import puppeteer from 'puppeteer';
const firefoxBrowser = await puppeteer.launch({
browser: 'firefox', // WebDriver BiDi is used by default.
});
const page = await firefoxBrowser.newPage();
...
await firefoxBrowser.close();
const chromeBrowser = await puppeteer.launch({
browser: 'chrome',
protocol: 'webDriverBiDi', // CDP would be used by default for Chrome.
});
const page = await chromeBrowser.newPage();
...
await chromeBrowser.close();
WebDriver BiDi 不支持 Puppeteer 功能
¥Puppeteer features not supported over WebDriver BiDi
-
各种模拟
¥Various emulations
-
Page.emulate()
-
Page.emulateCPUThrottling()
-
Page.emulateIdleState()
-
Page.emulateMediaFeatures()
-
Page.emulateMediaType()
-
Page.emulateTimezone()
-
Page.emulateVisionDeficiency()
-
Page.setBypassCSP()
-
Page.setGeolocation()
-
Page.setJavaScriptEnabled()
-
-
CDP 特定功能
¥CDP-specific features
-
HTTPRequest.client()
-
Page.createCDPSession()
-
-
无障碍
¥Accessibility
-
覆盖范围
¥Coverage
-
追踪
¥Tracing
-
其他方法:
¥Other methods:
-
Frame.waitForDevicePrompt()
-
HTTPResponse.buffer()
-
HTTPResponse.fromServiceWorker()
-
HTTPResponse.securityDetails()
-
Input.drag()
-
Input.dragAndDrop()
-
Input.dragOver()
-
Input.drop()
-
Page.emulateNetworkConditions()
-
Page.isDragInterceptionEnabled()
-
Page.isJavaScriptEnabled()
-
Page.isServiceWorkerBypassed()
-
Page.metrics()
-
Page.queryObjects()
-
Page.screencast()
-
Page.setBypassServiceWorker()
-
Page.setDragInterception()
-
Page.setOfflineMode()
-
Page.waitForDevicePrompt()
-
Page.waitForFileChooser()
-
PageEvent.popup
-
WebDriver BiDi 完全支持 Puppeteer 功能
¥Puppeteer features fully supported over WebDriver BiDi
-
浏览器自动化
¥Browser automation
-
Browser.close()
-
Browser.userAgent()
-
Browser.version()
-
Puppeteer.launch()
-
-
页面自动化
¥Page automation
-
Frame.goto()(
referer
和referrerPolicy
除外)¥Frame.goto() (except
referer
andreferrerPolicy
) -
Page '弹出窗口' 事件
¥Page 'popup' event
-
Page.bringToFront()
-
Page.cookies()
-
Page.deleteCookie()
-
Page.goBack()
-
Page.goForward()
-
Page.goto(
referer
和referrerPolicy
除外)¥Page.goto (except
referer
andreferrerPolicy
) -
Page.reload(
ignoreCache
参数除外)¥Page.reload (except for
ignoreCache
parameter) -
Page.setCacheEnabled()
-
Page.setCookie()
-
Page.setExtraHTTPHeaders()
-
Page.setViewport(仅限
width
、height
、deviceScaleFactor
)¥Page.setViewport (
width
,height
,deviceScaleFactor
only) -
Page.workers()
-
PageEvent.WorkerCreated
-
PageEvent.WorkerDestroyed
-
Target.opener()
-
-
脚本评估:
-
JSHandle.evaluate()
-
JSHandle.evaluateHandle()
-
Page.evaluate()
-
Page.evaluateOnNewDocument()
-
Page.exposeFunction()
-
-
¥Selectors and locators except for ARIA:
-
Page.$
-
Page.$$
-
Page.$$eval
-
Page.$eval
-
Page.waitForSelector
-
Page.locator() 和所有定位器 API
¥Page.locator() and all locator APIs
-
-
Input
-
ElementHandle.click
-
ElementHandle.uploadFile
-
Keyboard.down
-
Keyboard.press
-
Keyboard.sendCharacter
-
Keyboard.type
-
Keyboard.up
-
鼠标事件(专用的 drag'n'drop API 方法除外)
¥Mouse events (except for dedicated drag'n'drop API methods)
-
Page.tap
-
触摸屏 。*
¥TouchScreen.*
-
-
JavaScript 对话框拦截
¥JavaScript dialog interception
-
page.on('对话')
¥page.on('dialog')
-
对话。*
¥Dialog.*
-
-
截图(并非所有参数都支持)
¥Screenshots (not all parameters are supported)
-
Page.screenshot(支持的参数为
clip
、encoding
、fullPage
)¥Page.screenshot (supported parameters are
clip
,encoding
,fullPage
)
-
-
PDF 生成(并非所有参数都支持)
¥PDF generation (not all parameters are supported)
-
Page.pdf(仅支持
format
、height
、landscape
、margin
、pageRanges
、printBackground
、scale
、width
)¥Page.pdf (only
format
,height
,landscape
,margin
,pageRanges
,printBackground
,scale
,width
are supported) -
Page.createPDFStream(仅支持
format
、height
、landscape
、margin
、pageRanges
、printBackground
、scale
、width
)¥Page.createPDFStream (only
format
,height
,landscape
,margin
,pageRanges
,printBackground
,scale
,width
are supported)
-
-
权限
¥Permissions
-
BrowserContext.clearPermissionOverrides()
-
BrowserContext.overridePermissions()
-
-
-
HTTPRequest.abort() (no custom error support)
-
HTTPRequest.abortErrorReason()
-
HTTPRequest.continue()
-
HTTPRequest.continueRequestOverrides()
-
HTTPRequest.failure()
-
HTTPRequest.finalizeInterceptions()
-
HTTPRequest.interceptResolutionState()
-
HTTPRequest.isInterceptResolutionHandled()
-
HTTPRequest.response()
-
HTTPRequest.responseForRequest()
-
Page.authenticate()
-
Page.setRequestInterception()
-
Page.setUserAgent()
-
另请参阅
¥See also