可复制用例
以下五个用例对应的都是公开测试页面,安装打开 BrowserPilot 后即可直接通过 MCP、CLI 或 三语言 SDK 运行验证。
1. 抓 GitHub 话题榜单并导出 CSV
抓取 browser-automation 话题前 5 个仓库的名称、简介与链接:
python
import csv
from browserpilot import launch
with launch() as bp:
page = bp.new_page()
page.goto("https://github.com/topics/browser-automation")
page.locator("article").wait_for(timeout=20)
rows = page.evaluate("""(() =>
[...document.querySelectorAll('article')].slice(0, 5).map(card => {
const repo = (card.querySelector('h3')?.innerText || '').replace(/\\s+/g, ' ').trim();
const desc = (card.querySelector('p')?.innerText || '').trim();
return [repo, desc];
})
)()""")
with open("github-topics.csv", "w", newline="", encoding="utf-8-sig") as f:
writer = csv.writer(f)
writer.writerow(["仓库", "简介"])
writer.writerows(rows)
page.unlock()text
tab_open { "url": "https://github.com/topics/browser-automation" }
page_wait { "selector": "article", "timeout": 20 }
page_script { "source": "(() => [...document.querySelectorAll('article')].slice(0, 5).map(c => [(c.querySelector('h3')?.innerText || '').replace(/\\s+/g, ' ').trim(), (c.querySelector('p')?.innerText || '').trim()]))()" }
page_unlock {}若需长期复跑,将上述四步保存为「打开 → 等待 → 页面脚本 → 导出」工作流,导出的 CSV 自带 UTF-8 BOM,Excel 双击打开不乱码。
2. 填写表单并等待提交结果
使用 httpbin.org/forms/post 验证填表、单选/复选与 untilSelector 自动等待跳转(无需手写 sleep):
text
tab_open { "url": "https://httpbin.org/forms/post" }
page_wait { "selector": "input[name=custname]", "timeout": 15 }
page_type { "label": "Customer name", "text": "BrowserPilot Demo" }
page_type { "selector": "input[name=custemail]", "text": "demo@browserpilot.dev" }
page_click { "selector": "input[name=size][value=large]" }
page_click { "selector": "input[name=topping][value=cheese]" }
page_click { "selector": "button", "untilSelector": "pre" }
page_script { "source": "JSON.parse(document.querySelector('pre').innerText).form" }
page_unlock {}3. 按截图网格坐标精准点击
针对 Canvas 画板、地图或没有稳定 DOM 选择器的组件,利用 page_screenshot 返回的 shotId 直接传图片坐标点击,工具会自动处理视口偏移与 DPR 缩放:
text
page_screenshot { "grid": 50 }
# 假设从返回的网格图上读到目标中心在 (520, 340),且返回了 shotId: "shot-..."
page_click { "shotId": "shot-...", "x": 520, "y": 340 }如果需要连同地址栏、标签栏、遮罩与弹层一起留存整窗画面,调用:
text
window_preview { "grid": 100 }4. 新建隔离环境并绑定独立代理与时区
同时运营多区域站点账号时,新建独立环境并即时下发代理、时区与语言,不影响默认环境:
text
env_create { "name": "美区店铺", "remark": "独立代理与纽约时区环境" }
network_set {
"env": "env-刚刚返回的编号",
"proxy": { "mode": "fixed", "rules": "http://127.0.0.1:7890" },
"timezone": "America/New_York",
"acceptLanguages": "en-US,en",
"webrtcPolicy": "disable_non_proxied_udp"
}
tab_open { "env": "env-刚刚返回的编号", "url": "https://browserleaks.com/ip" }5. 系统计划任务 + CLI 零 Token 定时巡检
工作流保存为「话题巡检」(含输入参数 话题)后,准备参数文件 C:\tasks\watch.json:
json
{
"name": "话题巡检",
"inputs": {
"话题": "browser-automation"
}
}编写批处理脚本 C:\tasks\run-watch.bat 并注册到 Windows 计划任务(macOS / Linux 可用 crontab 或 launchd 执行相同命令):
bat
"%LOCALAPPDATA%\Programs\BrowserPilot\resources\cli\browserpilot.exe" call workflow_run --json-file C:\tasks\watch.jsonbat
schtasks /Create /TN "BrowserPilot 每日巡检" /SC DAILY /ST 09:00 /TR "C:\tasks\run-watch.bat"运行前提
定时巡检运行在用户图形桌面会话中(应用若未打开,CLI 会自动拉起并等待最多 45 秒);机器处于注销或无图形会话状态时无法渲染桌面窗口。