Browser Automation Ultra
Explore → Record → Replay → Fix. Convert expensive browser-tool interactions into zero-token Playwright scripts that reuse OpenClaw's Chrome session (cookies/login intact).
Prerequisites
Install Playwright (once per machine):
CODEBLOCK0
No browser download needed — scripts connect to OpenClaw's existing Chrome via CDP.
Architecture
CODEBLOCK1
Only one CDP client can connect at a time. browser-lock.sh handles the mutex.
Setup
- 1. Copy
scripts/browser-lock.sh to your workspace scripts/ directory - Copy
scripts/utils/human-like.js to your workspace INLINECODE4 - INLINECODE5
- Create
scripts/browser/ for your automation scripts
Core Workflow
1. Explore (browser tool, costs tokens)
Use the OpenClaw browser tool (snapshot/act) to figure out a workflow. Note selectors, page flow, key waits.
2. Record (write a Playwright script)
Convert steps into a script. Save to scripts/browser/<verb>-<target>.js. Use the template pattern:
CODEBLOCK2
3. Replay (zero tokens)
CODEBLOCK3
4. Fix (on error)
- 1. Read script error output
- Re-explore the failing step with
browser tool (snapshot) to check current UI - Update script with corrected selectors/logic
- Retry
Never guess fixes blindly. Always re-explore the actual page state.
browser-lock.sh
Manages CDP mutex between OpenClaw browser and Playwright scripts.
CODEBLOCK4
Lock file: /tmp/openclaw-browser.lock. Stale locks auto-recover.
Anti-Detection Rules (MANDATORY)
All scripts must use human-like.js. See references/anti-detection.md for the full rule set.
Summary of critical rules:
| ❌ Banned | ✅ Required |
|---|
| INLINECODE12 fixed delays | INLINECODE13 random range |
| INLINECODE14 instant fill |
humanType(page, sel, text) char-by-char with typos |
|
element.click() teleport click |
humanClick(page, sel) bezier mouse path + hover |
| Direct page operation after load |
humanBrowse(page) simulate reading first |
|
nativeSetter.call() DOM injection |
humanType() or
humanFillContentEditable() |
| Fixed cron schedule |
jitterWait(1, 10) random offset |
Exception: setInputFiles() for file uploads is allowed (no human simulation possible), but add random delays before/after.
human-like.js API
| Function | Purpose |
|---|
| INLINECODE24 | Random wait (ms) |
| INLINECODE25 |
Longer pause before form fills |
|
humanClick(page, sel) | Bezier mouse move → hover → click with press/release jitter |
|
humanType(page, sel, text, opts) | Char-by-char typing, normal distribution speed, 3% typo rate |
|
humanFillContentEditable(page, sel, text) | For contenteditable divs (line-by-line Enter + humanType) |
|
humanBrowse(page, opts) | Simulate page reading (scroll + mouse wander, 2-5s) |
|
humanScroll(page, opts) | Random scroll with occasional reverse |
|
jitterWait(minMin, maxMin) | Random delay in minutes for cron tasks |
Script Naming Convention
INLINECODE32 — e.g. publish-deviantart.js, read-inbox.js, INLINECODE35
Example Scripts
Production-tested scripts in scripts/examples/. Copy to your workspace scripts/browser/ and adapt.
| Script | Platform | Function |
|---|
| INLINECODE38 | DeviantArt | Upload image, fill title/desc/tags, submit |
| INLINECODE39 |
小红书 | Publish image note with topic tag association via recommend list |
|
publish-pinterest.js | Pinterest | Create pin with title/desc, select board |
|
publish-behance.js | Behance | Upload project with title/desc/tags/categories |
|
read-proton-latest.js | Proton Mail | Read inbox, output JSON list of emails |
|
read-xhs-comments.js | 小红书 | Read notification comments, output JSON with reply button index |
|
reply-xhs-comment.js | 小红书 | Reply to a specific comment by index |
Usage pattern:
CODEBLOCK5
All example scripts already use human-like.js for anti-detection.
Cron Integration
CODEBLOCK6
Add jitterWait() at script start to randomize execution time.
Troubleshooting
| Problem | Fix |
|---|
| Lock held by PID xxx | INLINECODE47 |
| CDP connection timeout |
Ensure
acquire was called / Chrome is running |
| Login expired | Use browser tool to re-login, then run script |
| Selector not found | Re-explore with browser tool, update script |
| Script timeout | Increase with
--timeout flag |
Environment Variables
| Var | Default | Description |
|---|
| INLINECODE50 | auto-discover | Override CDP port |
| INLINECODE51 |
auto-detect | Chrome binary path |
|
HEADLESS | auto |
true/
false to force headless |
Browser Automation Ultra
探索 → 录制 → 回放 → 修复。将昂贵的浏览器工具交互转换为零令牌的 Playwright 脚本,复用 OpenClaw 的 Chrome 会话(保持 Cookie/登录状态)。
前置条件
安装 Playwright(每台机器一次):
bash
npm install -g playwright
或在工作区中:npm init -y && npm install playwright
无需下载浏览器——脚本通过 CDP 连接到 OpenClaw 现有的 Chrome。
架构
Chrome 用户数据:~/.openclaw/browser/openclaw/user-data
↕ 共享 Cookie/登录(互斥 CDP)
┌──────────────┐ ┌──────────────────┐
│ 浏览器工具 │ 或 │ Playwright 脚本 │
│ (探索) │ │ (零令牌) │
└──────────────┘ └──────────────────┘
↕ 由 browser-lock.sh 管理
一次只能连接一个 CDP 客户端。browser-lock.sh 处理互斥锁。
设置
- 1. 将 scripts/browser-lock.sh 复制到工作区的 scripts/ 目录
- 将 scripts/utils/human-like.js 复制到工作区的 scripts/browser/utils/
- chmod +x scripts/browser-lock.sh
- 创建 scripts/browser/ 目录存放自动化脚本
核心工作流
1. 探索(浏览器工具,消耗令牌)
使用 OpenClaw 的 browser 工具(快照/操作)来找出工作流程。记录选择器、页面流程和关键等待点。
2. 录制(编写 Playwright 脚本)
将步骤转换为脚本。保存到 scripts/browser/<动词>-<目标>.js。使用模板模式:
javascript
const { chromium } = require(playwright);
const { humanDelay, humanClick, humanType, humanThink, humanBrowse } = require(./utils/human-like);
function discoverCdpUrl() {
try {
const { execSync } = require(child_process);
const ps = execSync(ps aux | grep remote-debugging-port | grep -v grep, { encoding: utf8 });
const match = ps.match(/remote-debugging-port=(\d+)/);
return http://127.0.0.1:${match ? match[1] : 18800};
} catch { return http://127.0.0.1:18800; }
}
async function main() {
const browser = await chromium.connectOverCDP(discoverCdpUrl());
const context = browser.contexts()[0]; // 复用现有上下文(Cookie/登录)
const page = await context.newPage();
try {
// 在此处编写自动化代码——使用类人函数
await page.goto(https://example.com, { waitUntil: networkidle, timeout: 30000 });
await humanBrowse(page); // 模拟浏览页面
await humanClick(page, button.submit);
await humanType(page, input[name=title], Hello World);
} finally {
await page.close(); // 永远不要调用 browser.close()——会关闭整个 Chrome
}
}
main().then(() => process.exit(0)).catch(e => { console.error(❌, e.message); process.exit(1); });
3. 回放(零令牌)
bash
./scripts/browser-lock.sh run scripts/browser/my-task.js [参数]
./scripts/browser-lock.sh run --timeout 120 scripts/browser/my-task.js
4. 修复(出错时)
- 1. 读取脚本错误输出
- 使用 browser 工具(快照)重新探索失败的步骤,检查当前 UI
- 用更正后的选择器/逻辑更新脚本
- 重试
切勿盲目猜测修复。始终重新探索实际页面状态。
browser-lock.sh
管理 OpenClaw 浏览器和 Playwright 脚本之间的 CDP 互斥锁。
bash
./scripts/browser-lock.sh run [参数] # 获取 → 运行 → 释放(默认 300 秒)
./scripts/browser-lock.sh run --timeout 120