Clawbrowser – browser control via Playwright CLI
Setup & orientation
- 1. Install the CLI and verify availability:
npm install -g @playwright/cli@latest
playwright-cli --help
The CLI is headless by default; add
--headed to
open or set
browser.launchOptions.headless to
false in
playwright-cli.json when you need to see the UI.
- 2. The CLI reads
playwright-cli.json by default or whatever file you pass with --config. Use the config to tune browser name, launch/context options, viewport, timeouts, output directories, and recording settings without changing every command. - Keep
playwright-cli --help terminal-accessible; the script self-documents the latest commands and options so you can refresh your memory before trying a new action.
Core interaction loop
- 1. Start with
playwright-cli open <url> to load the page (add --session=name if you want isolation up front). - Run
playwright-cli snapshot to generate element refs (e1, e2, …) before any interaction. Always re-snapshot after DOM changes or navigation to avoid stale refs. - Use refs for actions:
-
click,
dblclick,
hover,
drag,
check,
uncheck,
select,
fill,
type,
upload,
eval
- Append
[button],
[value], or JS snippets as needed (e.g.,
playwright-cli click e4 right).
- 4. Capture output evidence with
screenshot [ref], pdf, console [level], or network to prove the flow or inspect errors. - Example flow:
CODEBLOCK1
Sessions & persistence
- - Use
--session=<name> to keep cookies, storage, and tabs isolated per workflow. Sessions behave like persistent profiles: they remember auth state, history, and tabs between commands. - Export
PLAYWRIGHT_CLI_SESSION=mysession if you are running many commands in the same session — the CLI will default to that session without needing --session each time. - Manage sessions explicitly:
playwright-cli session-list
playwright-cli session-stop <name>
playwright-cli session-stop-all
playwright-cli session-restart <name>
playwright-cli session-delete <name>
- - Use
playwright-cli --isolated open ... for ephemeral contexts that do not persist to disk. - Whenever you change browser settings for a session (launch args, headless toggle, browser selection), rerun
playwright-cli config for that session and then session-restart to apply the new config.
Tabs, navigation, and devtools
- - Tab helpers:
tab-list, tab-new [url], tab-close <index>, tab-select <index>. - Navigation shortcuts:
go-back, go-forward, reload. - Keyboard and mouse control:
press <key>, keydown, keyup, mousemove <x> <y>, mousedown [button], mouseup [button], mousewheel <dx> <dy>. - Devtools-style introspection:
playwright-cli console [level]
playwright-cli network
playwright-cli run-code "async page => await page.context().grantPermissions(['clipboard-read'])"
Use these to check console logs, inspect network requests, or inject helper scripts.
Recording, tracing, and exports
- - Record traces and videos around delicate interactions so you can replay what the agent did later:
playwright-cli tracing-start
# perform steps
playwright-cli tracing-stop
playwright-cli video-start
# perform steps
playwright-cli video-stop video.webm
- - Save evidence to disk with
screenshot, pdf, or snapshot (which dumps element refs). Recorded files honor the outputDir from your config.
Config, state, and housekeeping
- - Use
playwright-cli config to tweak runtime flags without reinstalling. Examples:
playwright-cli config --headed --browser=firefox
playwright-cli --session=auth config --config=playwright-cli.json
Change
browser,
contextOptions,
launchOptions, or recording settings in the config and restart the session to apply them.
- - Running
playwright-cli install refreshes browser binaries if the environment is new or you receive errors about missing binaries. - Clean up sessions when finished to avoid stale state:
CODEBLOCK6
Troubleshooting & reminders
- - If a command fails, rerun
playwright-cli snapshot to confirm refs are still valid. Snapshots provide the current DOM context for click/type operations. - INLINECODE63 always shows the latest command set, so consult it before trying a rarely used flag.
- When the agent needs to replicate a recorded manual flow, capture a screenshot, note the session name, and mention which refs and tabs were in use.
- If targeting a visible browser is required (e.g., manual inspection), reconfigure with
--headed, or run playwright-cli open --headed <url> for that session only.
Clawbrowser – 通过Playwright CLI控制浏览器
安装与配置
- 1. 安装CLI并验证可用性:
bash
npm install -g @playwright/cli@latest
playwright-cli --help
CLI默认以无头模式运行;当需要查看UI时,可在open命令后添加--headed参数,或在playwright-cli.json中将browser.launchOptions.headless设置为false。
- 2. CLI默认读取playwright-cli.json,也可通过--config参数指定其他配置文件。使用配置文件可调整浏览器名称、启动/上下文选项、视口、超时时间、输出目录和录制设置,无需修改每条命令。
- 保持playwright-cli --help终端可访问;该脚本会自文档化最新的命令和选项,以便在尝试新操作前刷新记忆。
核心交互流程
- 1. 使用playwright-cli open 加载页面(如需预先隔离,可添加--session=name参数)。
- 在任何交互前运行playwright-cli snapshot生成元素引用(e1、e2等)。DOM变化或导航后务必重新生成快照,避免引用失效。
- 使用引用执行操作:
- click、dblclick、hover、drag、check、uncheck、select、fill、type、upload、eval
- 根据需要附加[button]、[value]或JS代码片段(例如playwright-cli click e4 right)。
- 4. 使用screenshot [ref]、pdf、console [level]或network捕获输出证据,以验证流程或检查错误。
- 示例流程:
bash
playwright-cli open https://example.com/login
playwright-cli snapshot
playwright-cli fill e1 user@example.com
playwright-cli fill e2 supersecret
playwright-cli click e3
playwright-cli snapshot
playwright-cli screenshot
会话与持久化
- - 使用--session=为每个工作流隔离保存cookie、存储和标签页。会话行为类似持久化配置文件:会记住命令间的认证状态、历史和标签页。
- 如果在同一会话中运行多条命令,可导出PLAYWRIGHTCLISESSION=mysession环境变量——CLI将默认使用该会话,无需每次指定--session。
- 显式管理会话:
bash
playwright-cli session-list
playwright-cli session-stop
playwright-cli session-stop-all
playwright-cli session-restart
playwright-cli session-delete
- - 使用playwright-cli --isolated open ...创建临时上下文,不会持久化到磁盘。
- 每当更改会话的浏览器设置(启动参数、无头模式切换、浏览器选择)时,需重新运行playwright-cli config为该会话配置,然后执行session-restart应用新配置。
标签页、导航与开发者工具
- - 标签页辅助功能:tab-list、tab-new [url]、tab-close 、tab-select 。
- 导航快捷方式:go-back、go-forward、reload。
- 键盘和鼠标控制:press 、keydown、keyup、mousemove 、mousedown [button]、mouseup [button]、mousewheel 。
- 开发者工具式内省:
bash
playwright-cli console [level]
playwright-cli network
playwright-cli run-code async page => await page.context().grantPermissions([clipboard-read])
使用这些命令检查控制台日志、查看网络请求或注入辅助脚本。
录制、追踪与导出
- - 在关键交互周围录制追踪和视频,以便后续回放代理的操作:
bash
playwright-cli tracing-start
# 执行步骤
playwright-cli tracing-stop
playwright-cli video-start
# 执行步骤
playwright-cli video-stop video.webm
- - 使用screenshot、pdf或snapshot(导出元素引用)将证据保存到磁盘。录制文件遵循配置中的outputDir设置。
配置、状态与维护
- - 使用playwright-cli config调整运行时标志,无需重新安装。示例:
bash
playwright-cli config --headed --browser=firefox
playwright-cli --session=auth config --config=playwright-cli.json
在配置中更改browser、contextOptions、launchOptions或录制设置,然后重启会话以应用更改。
- - 如果环境是新的或收到缺少浏览器二进制文件的错误,运行playwright-cli install刷新浏览器二进制文件。
- 完成后清理会话以避免过期状态:
bash
playwright-cli session-stop
playwright-cli session-delete
故障排除与提醒
- - 如果命令执行失败,重新运行playwright-cli snapshot确认引用仍然有效。快照为click/type操作提供当前DOM上下文。
- playwright-cli --help始终显示最新的命令集,因此在尝试不常用的标志前请先查阅。
- 当代理需要复现录制的手动流程时,请截取屏幕截图、记录会话名称,并说明正在使用的引用和标签页。
- 如果需要定位可见浏览器(例如手动检查),可重新配置--headed参数,或仅对该会话运行playwright-cli open --headed 。