浏览器自动化是OpenClaw的扩展能力,通过集成浏览器控制功能,让AI助手能够像真人一样操作网页、提取数据、执行交互任务。以下是详细的功能介绍和操作指南。

浏览器自动化的核心能力

1. 网页数据抓取与监控

  • 实时数据采集:股票价格、商品库存、新闻动态、汇率变化
  • 价格监控:电商平台价格跟踪,设置降价提醒
  • 竞品分析:自动收集竞争对手的产品信息和定价
  • 内容聚合:从多个新闻源抓取特定主题文章

2. 自动化测试与质量保证

  • 功能测试:自动执行网页功能回归测试
  • 兼容性测试:跨浏览器、跨设备页面渲染检查
  • 性能监控:页面加载速度、资源加载情况监测
  • 表单验证:自动填写并提交表单测试

3. 工作流程自动化

  • 日常操作:自动登录系统、下载报表、提交工单
  • 数据录入:将Excel/CSV数据批量录入网页系统
  • 文件处理:自动下载、重命名、分类存储文件
  • 定时任务:每天固定时间执行网页操作

4. 交互式网页操作

  • 点击与导航:模拟用户点击按钮、链接、菜单
  • 表单填写:自动输入文本、选择选项、上传文件
  • 滚动与等待:处理动态加载内容,等待元素出现
  • 截图与录屏:记录操作过程,生成操作报告

5. 高级数据处理

  • 结构化提取:从表格、列表、卡片中提取规整数据
  • 文本分析:抓取页面文本进行关键词提取、情感分析
  • 图像识别:配合OCR技术读取图片中的文字信息
  • API模拟:分析网页请求,直接调用后端API提高效率

安装与配置浏览器自动化

前提条件

  1. 操作系统:Windows 10/11,macOS 10.15+,Linux Ubuntu 18.04+
  2. Node.js:版本22或更高
  3. OpenClaw:2026.2.26及以上版本
  4. 浏览器:Chrome/Edge 120+ 或 Firefox 115+

安装浏览器自动化技能

方法一:通过ClawHub安装(推荐)

  1. # 搜索浏览器相关技能
  2. clawhub search browser
  3. clawhub search puppeteer
  4. clawhub search playwright
  5. # 安装主技能包
  6. clawhub install browser-automation
  7. # 安装扩展技能
  8. clawhub install web-scraper
  9. clawhub install form-autofill
  10. clawhub install screenshot-tools

方法二:手动安装Playwright集成

  1. # 进入技能目录
  2. cd ~/.openclaw/workspace/skills
  3. # 克隆浏览器自动化技能
  4. git clone https://github.com/openclaw/skill-browser-automation.git
  5. # 安装Playwright浏览器
  6. cd skill-browser-automation
  7. npm install
  8. npx playwright install chromium # 安装Chromium
  9. npx playwright install firefox # 安装Firefox(可选)
  10. npx playwright install webkit # 安装WebKit(可选)

方法三:安装Puppeteer方案

  1. # 安装Puppeteer技能
  2. clawhub install puppeteer-control
  3. # 或手动安装
  4. cd ~/.openclaw/workspace/skills
  5. git clone https://github.com/openclaw/skill-puppeteer.git
  6. cd skill-puppeteer
  7. npm install puppeteer

配置浏览器环境

  1. # 设置浏览器路径(如使用自定义Chrome)
  2. openclaw config set browser.path "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome"
  3. # 配置浏览器参数
  4. openclaw config set browser.args '["--no-sandbox", "--disable-setuid-sandbox"]'
  5. # 设置用户数据目录(保持登录状态)
  6. openclaw config set browser.userDataDir "~/.openclaw/browser-profile"
  7. # 配置代理(如需要)
  8. openclaw config set browser.proxy "http://127.0.0.1:7890"

基础操作指南

1. 启动浏览器会话

  1. # 通过OpenClaw命令启动
  2. openclaw browser start
  3. # 指定浏览器类型
  4. openclaw browser start --browser chromium
  5. openclaw browser start --browser firefox
  6. # 无头模式(不显示界面)
  7. openclaw browser start --headless
  8. # 指定视口大小
  9. openclaw browser start --viewport "1920,1080"

2. 基本网页操作命令

在OpenClaw聊天窗口中,可以直接使用自然语言控制浏览器:

  1. "打开百度首页"
  2. "在搜索框输入OpenClaw浏览器自动化"
  3. "点击搜索按钮"
  4. "滚动到页面底部"
  5. "截取整个页面保存为screenshot.png"
  6. "提取前10个搜索结果标题和链接"
  7. "点击下一页"
  8. "关闭浏览器"

3. 自动化脚本示例

创建自动化脚本文件 automation.mjs

  1. // 示例:自动登录并抓取数据
  2. export default async function automate(browser) {
  3. const page = await browser.newPage();
  4. // 1. 访问登录页面
  5. await page.goto('https://example.com/login');
  6. // 2. 填写登录表单
  7. await page.type('#username', 'your_username');
  8. await page.type('#password', 'your_password');
  9. await page.click('#login-button');
  10. // 3. 等待登录完成
  11. await page.waitForNavigation();
  12. // 4. 访问目标页面
  13. await page.goto('https://example.com/dashboard');
  14. // 5. 提取数据
  15. const data = await page.evaluate(() => {
  16. const items = document.querySelectorAll('.data-item');
  17. return Array.from(items).map(item => ({
  18. title: item.querySelector('.title').textContent,
  19. value: item.querySelector('.value').textContent
  20. }));
  21. });
  22. // 6. 保存数据
  23. await browser.saveData(data, 'extracted_data.json');
  24. // 7. 截图记录
  25. await page.screenshot({ path: 'dashboard.png' });
  26. return data;
  27. }

实际应用场景与操作

场景一:电商价格监控

  1. # 创建价格监控任务
  2. openclaw automation create --name "price-monitor" --schedule "0 */6 * * *"
  3. # 监控脚本示例
  4. cat > ~/.openclaw/automations/price-monitor.mjs << 'EOF'
  5. export default async function monitor(browser) {
  6. const page = await browser.newPage();
  7. // 监控亚马逊商品价格
  8. await page.goto('https://www.amazon.com/dp/B0XXXXXXX');
  9. const price = await page.$eval('#priceblock_ourprice', el => el.textContent);
  10. const title = await page.$eval('#productTitle', el => el.textContent.trim());
  11. // 价格低于阈值时通知
  12. const currentPrice = parseFloat(price.replace('$', ''));
  13. if (currentPrice < 99.99) {
  14. await browser.notify(`价格下降: ${title} 现在仅售 ${price}`);
  15. }
  16. // 保存历史价格
  17. await browser.appendData({
  18. date: new Date().toISOString(),
  19. product: title,
  20. price: currentPrice
  21. }, 'price-history.json');
  22. return { title, price: currentPrice };
  23. }
  24. EOF

场景二:自动填写日报

  1. # 日报自动化配置
  2. openclaw config set automations.daily-report.enabled true
  3. openclaw config set automations.daily-report.time "18:00"
  4. openclaw config set automations.daily-report.days "[1,2,3,4,5]" # 周一到周五
  5. # 日报脚本
  6. cat > ~/.openclaw/automations/daily-report.mjs << 'EOF'
  7. export default async function fillReport(browser, context) {
  8. const page = await browser.newPage();
  9. // 登录公司系统
  10. await page.goto('https://internal.company.com/login');
  11. await page.type('#username', process.env.COMPANY_USER);
  12. await page.type('#password', process.env.COMPANY_PASS);
  13. await page.click('#submit');
  14. // 进入日报页面
  15. await page.waitForNavigation();
  16. await page.goto('https://internal.company.com/daily-report');
  17. // 读取今日工作内容(从本地文件)
  18. const todayWork = await context.readFile('today-work.md');
  19. // 填写日报
  20. await page.type('#work-content', todayWork);
  21. // 选择项目
  22. await page.select('#project', 'PROJECT-001');
  23. // 填写工时
  24. await page.type('#hours', '8');
  25. // 明日计划
  26. const tomorrowPlan = await context.generateText('生成明日工作计划');
  27. await page.type('#tomorrow-plan', tomorrowPlan);
  28. // 提交
  29. await page.click('#submit-report');
  30. // 确认提交成功
  31. await page.waitForSelector('.success-message', { timeout: 5000 });
  32. return { status: 'success', time: new Date().toISOString() };
  33. }
  34. EOF

场景三:竞品数据收集

  1. # 多网站数据收集配置
  2. openclaw config set automations.competitor-monitor.sites '["site1.com", "site2.com", "site3.com"]'
  3. openclaw config set automations.competitor-monitor.interval 3600 # 每小时运行
  4. # 收集脚本
  5. cat > ~/.openclaw/automations/competitor-monitor.mjs << 'EOF'
  6. export default async function collectCompetitorData(browser) {
  7. const results = [];
  8. const sites = [
  9. 'https://competitor1.com/products',
  10. 'https://competitor2.com/offerings',
  11. 'https://competitor3.com/catalog'
  12. ];
  13. for (const site of sites) {
  14. const page = await browser.newPage();
  15. await page.goto(site);
  16. // 提取产品信息
  17. const products = await page.evaluate(() => {
  18. const items = document.querySelectorAll('.product-item');
  19. return Array.from(items).map(item => ({
  20. name: item.querySelector('.name')?.textContent || '',
  21. price: item.querySelector('.price')?.textContent || '',
  22. description: item.querySelector('.desc')?.textContent || '',
  23. url: item.querySelector('a')?.href || ''
  24. }));
  25. });
  26. results.push({
  27. site: new URL(site).hostname,
  28. timestamp: new Date().toISOString(),
  29. productCount: products.length,
  30. products: products.slice(0, 10) // 只取前10
  31. });
  32. await page.close();
  33. }
  34. // 数据分析
  35. const analysis = await browser.analyzeData(results, {
  36. type: 'competitor_analysis',
  37. metrics: ['price_comparison', 'feature_analysis']
  38. });
  39. // 生成报告
  40. const report = await browser.generateReport({
  41. data: results,
  42. analysis: analysis,
  43. format: 'markdown'
  44. });
  45. await browser.saveData(report, `competitor-report-${Date.now()}.md`);
  46. return { collected: results.length, analysis: analysis.summary };
  47. }
  48. EOF

高级功能配置

1. 处理验证码和反爬机制

  1. // 配置反反爬策略
  2. openclaw config set browser.stealth true
  3. openclaw config set browser.delay "1000-3000" // 随机延迟1-3秒
  4. // 使用代理池
  5. openclaw config set browser.proxyPool.enabled true
  6. openclaw config set browser.proxyPool.list '["proxy1:port", "proxy2:port"]'
  7. // 验证码处理(需要第三方服务)
  8. openclaw config set browser.captcha.service "2captcha"
  9. openclaw config set browser.captcha.apiKey "your_2captcha_key"

2. 数据存储与处理管道

  1. # 配置数据存储
  2. openclaw config set automation.storage.type "json" # json, csv, database
  3. openclaw config set automation.storage.path "./data"
  4. openclaw config set automation.storage.backup true
  5. # 设置数据处理管道
  6. openclaw config set automation.pipeline.enabled true
  7. openclaw config set automation.pipeline.steps '["clean", "deduplicate", "enrich", "export"]'

3. 错误处理与重试机制

  1. // 自动化脚本中的错误处理
  2. export default async function robustAutomation(browser) {
  3. const maxRetries = 3;
  4. let retries = 0;
  5. while (retries < maxRetries) {
  6. try {
  7. const page = await browser.newPage();
  8. await page.goto('https://example.com', {
  9. timeout: 30000,
  10. waitUntil: 'networkidle2'
  11. });
  12. // 页面操作...
  13. return await extractData(page);
  14. } catch (error) {
  15. retries++;
  16. console.error(`尝试 ${retries}/${maxRetries} 失败:`, error.message);
  17. if (retries >= maxRetries) {
  18. await browser.notify(`自动化任务失败: ${error.message}`);
  19. throw error;
  20. }
  21. // 等待后重试
  22. await browser.sleep(5000 * retries);
  23. }
  24. }
  25. }

4. 性能优化配置

  1. # 浏览器资源限制
  2. openclaw config set browser.resourceLimits.cpu 0.5 # 50% CPU限制
  3. openclaw config set browser.resourceLimits.memory "1G" # 内存限制
  4. openclaw config set browser.resourceLimits.timeout 300 # 5分钟超时
  5. # 并发控制
  6. openclaw config set browser.concurrency.max 3 # 最大并发页面数
  7. openclaw config set browser.concurrency.delay 1000 # 页面间延迟
  8. # 缓存配置
  9. openclaw config set browser.cache.enabled true
  10. openclaw config set browser.cache.ttl 3600 # 缓存1小时

监控与管理

1. 查看自动化任务状态

  1. # 列出所有自动化任务
  2. openclaw automation list
  3. # 查看任务详情
  4. openclaw automation info <任务名>
  5. # 查看任务日志
  6. openclaw automation logs <任务名>
  7. openclaw automation logs <任务名> --tail 50
  8. openclaw automation logs <任务名> --follow
  9. # 任务统计
  10. openclaw automation stats
  11. openclaw automation stats --period "7d"

2. 任务控制命令

  1. # 启动任务
  2. openclaw automation start <任务名>
  3. openclaw automation start-all
  4. # 停止任务
  5. openclaw automation stop <任务名>
  6. openclaw automation stop-all
  7. # 立即运行一次
  8. openclaw automation run <任务名>
  9. # 暂停/恢复任务
  10. openclaw automation pause <任务名>
  11. openclaw automation resume <任务名>
  12. # 删除任务
  13. openclaw automation remove <任务名>

3. 性能监控

  1. # 查看浏览器资源使用
  2. openclaw browser stats
  3. # 监控自动化任务性能
  4. openclaw automation monitor <任务名>
  5. # 生成性能报告
  6. openclaw automation report <任务名> --period "30d"

安全与最佳实践

1. 安全配置

  1. # 限制访问域名
  2. openclaw config set browser.allowedDomains '["example.com", "api.example.com"]'
  3. # 禁用危险功能
  4. openclaw config set browser.security.disableWebSecurity false
  5. openclaw config set browser.security.ignoreHTTPSErrors false
  6. # 设置内容安全策略
  7. openclaw config set browser.security.csp "default-src 'self'"
  8. # 数据清理配置
  9. openclaw config set browser.privacy.clearCookies true
  10. openclaw config set browser.privacy.clearCache true

2. 合规性注意事项

  • 遵守robots.txt:尊重网站的爬虫协议
  • 设置合理频率:避免对目标网站造成压力
  • 用户代理标识:使用可识别的User-Agent
  • 数据使用许可:确保有权利使用抓取的数据
  • 个人隐私保护:不收集个人信息,遵守GDPR等法规

3. 维护建议

  1. # 定期更新浏览器和驱动
  2. npx playwright install --with-deps
  3. npm update puppeteer
  4. # 清理临时文件
  5. openclaw browser cleanup
  6. # 备份重要数据
  7. openclaw automation backup --output ./backup/
  8. # 监控任务健康
  9. openclaw health check --automation

故障排除

常见问题解决

  1. 浏览器启动失败

    1. # 检查浏览器安装
    2. openclaw browser check
    3. # 重新安装浏览器
    4. npx playwright install
    5. # 清理浏览器缓存
    6. openclaw browser cleanup --cache
  2. 页面加载超时

    1. // 增加超时时间
    2. await page.goto(url, { timeout: 60000 });
    3. // 调整等待策略
    4. await page.goto(url, { waitUntil: 'domcontentloaded' });
    5. // 使用更宽松的等待条件
    6. await page.waitForFunction(() => document.readyState === 'complete');
  3. 元素找不到

    1. // 使用多种选择器
    2. await page.waitForSelector('.class1, .class2, #id');
    3. // 增加等待时间
    4. await page.waitForSelector('.element', { timeout: 10000 });
    5. // 检查iframe
    6. const frame = page.frames().find(f => f.name() === 'frame-name');
    7. await frame.waitForSelector('.element');
  4. 反爬虫检测

    1. # 启用隐身模式
    2. openclaw config set browser.stealth true
    3. # 使用真实User-Agent
    4. openclaw config set browser.userAgent "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36"
    5. # 添加随机延迟
    6. openclaw config set browser.randomDelay "2000-5000"

通过以上配置和操作,你可以充分利用OpenClaw的浏览器自动化能力,实现各种网页操作任务的自动化。建议从简单任务开始,逐步增加复杂度,并始终注意遵守目标网站的使用条款。

© 本文著作权归作者所有。转载请联系授权,禁止商用。

🔗 系列文章

1. openclaw能做什么?

2. openclaw会不会窃取我电脑上的私密信息?

3. openclaw的沙盒模式是什么?

4. Windows环境下如何正确安装OpenClaw?

5. 安装后提示"command not found"怎么办?

6. Node.js版本要求是什么?为什么推荐22版本?

7. 端口18789被占用如何处理?

8. 如何配置飞书/钉钉等国内聊天平台?

9. 配对码(Pairing)是什么?如何批准连接?

10. 如何切换AI模型提供商?

11. 联网搜索功能如何配置?

12. OpenClaw的记忆功能为什么"不会记住对话"?​

13. 如何安装和管理Skills(技能)?​

14. 定时任务(Cron Jobs)如何设置?

15. 浏览器自动化能做什么?具体如何操作?

16. 如何防范提示词注入(Prompt Injection)攻击?

17. 如何识别和避免恶意Skills?

18. 使用OpenClaw每月需要多少费用?

19. 如何控制Token消耗成本?

20. Gateway服务启动失败如何排查?

21. 遇到"HTTP 401: invalid access token"等错误怎么办?