Playwright 其他 API
本附录列出 Dialog、Download、Video、Tracing 等辅助类的 API。
Dialog 类
| 方法 | 说明 |
|---|---|
dialog.accept(promptText?) | 确认弹窗 |
dialog.dismiss() | 取消弹窗 |
dialog.message() | 弹窗消息 |
dialog.type() | 弹窗类型:'alert' | 'confirm' | 'prompt' | 'beforeunload' |
dialog.defaultValue() | 弹窗默认输入值 |
dialog.page() | 弹窗所属页面 |
Download 类
| 方法 | 说明 |
|---|---|
download.saveAs(path) | 保存文件到指定路径 |
download.suggestedFilename() | 获取建议的文件名 |
download.url() | 下载的 URL |
download.page() | 所属页面 |
download.failure() | 下载失败信息(无失败返回 null) |
download.delete() | 删除下载的文件 |
FileChooser 类
| 方法 | 说明 |
|---|---|
fileChooser.setFiles(files, options?) | 选择文件 |
fileChooser.isMultiple() | 是否多选模式 |
fileChooser.page() | 所属页面 |
ConsoleMessage 类
| 方法 | 说明 |
|---|---|
consoleMessage.type() | 消息类型:'log' | 'error' | 'warning' | 'info' | 'debug' |
consoleMessage.text() | 消息文本 |
consoleMessage.location() | 消息来源位置 |
consoleMessage.args() | 消息参数(JSHandle 数组) |
Video 类
| 方法 | 说明 |
|---|---|
video.saveAs(path) | 保存视频到文件 |
video.delete() | 删除视频文件 |
video.path() | 视频文件路径 |
Tracing 类
| 方法 | 说明 |
|---|---|
tracing.start(options?) | 开始录制 Trace |
tracing.startChunk(options?) | 开始新的 Trace 块 |
tracing.stopChunk(options?) | 停止 Trace 块并保存 |
tracing.stop(options?) | 停止录制并保存 Trace |
Clock 类
| 方法 | 说明 |
|---|---|
clock.install(options?) | 安装假时钟 |
clock.pauseAt(time) | 暂停在指定时间 |
clock.resume() | 恢复执行 |
clock.setFixedTime(time) | 设置固定时间 |
clock.fastForward(time) | 快进时间 |
clock.runFor(time) | 运行指定时间 |
Selectors 类
| 方法 | 说明 |
|---|---|
selectors.register(name, script, options?) | 注册自定义选择器引擎 |
ElementHandle 类
| 方法 | 说明 |
|---|---|
elementHandle.click(options?) | 点击 |
elementHandle.fill(value) | 填入文本 |
elementHandle.screenshot(options?) | 元素截图 |
elementHandle.boundingBox() | 获取位置大小 |
elementHandle.getAttribute(name) | 获取属性 |
elementHandle.textContent() | 获取文本 |
elementHandle.ownerFrame() | 所属 Frame |
JSHandle 类
| 方法 | 说明 |
|---|---|
jsHandle.evaluate(fn, arg?) | 执行 JS |
jsHandle.getProperty(name) | 获取属性 |
jsHandle.getProperties() | 获取所有属性 |
jsHandle.jsonValue() | 获取 JSON 值 |
jsHandle.dispose() | 释放资源 |
CDPSession 类
| 方法 | 说明 |
|---|---|
cdpSession.send(method, params?) | 发送 CDP 命令 |
cdpSession.detach() | 分离 CDP 会话 |
Worker 类
| 方法/事件 | 说明 |
|---|---|
worker.url() | Worker URL |
worker.evaluate(fn, arg?) | 在 Worker 中执行 JS |
worker.on('close', handler) | Worker 关闭事件 |
Frame 类
| 方法 | 说明 |
|---|---|
frame.name() | Frame 名称 |
frame.url() | Frame URL |
frame.goto(url, options?) | Frame 导航 |
frame.locator(selector) | CSS/XPath 定位 |
frame.getByRole(role, options?) | 按角色定位 |
frame.getByText(text) | 按文本定位 |
frame.evaluate(fn, arg?) | 执行 JS |
frame.content() | 获取 HTML 内容 |
frame.parentFrame() | 父 Frame |
frame.childFrames() | 子 Frame 列表 |
错误类
| 类 | 说明 |
|---|---|
PlaywrightException | 通用 Playwright 异常 |
TimeoutError | 超时错误(断言超时、操作超时等) |
