BrowserPilot快速上手教程:3步配置Chromedriver与OpenAI,5分钟跑通自然语言浏览器自动化
【免费下载链接】browserpilotNatural language browser automation项目地址: https://gitcode.com/gh_mirrors/br/browserpilot
BrowserPilot是一个用自然语言控制浏览器的开源工具:你只需用英语写下"打开谷歌、点击搜索框、输入关键词"这样的指令,它就会自动打开 Chrome 浏览器替你完成网页操作、信息提取甚至自动登录,非常适合做网页自动化、数据抓取和爬虫测试。整个上手流程只有 3 个配置步骤,5 分钟即可跑通第一个自然语言浏览器自动化任务。
一、为什么选择 BrowserPilot?
传统浏览器自动化(如手写 Selenium 脚本)最大的痛点是:网页稍有改版,脚本就报废。
BrowserPilot 的思路完全不同——它把 GPT 接在了 Selenium 上:
- 📝 你写的不是代码,而是一行行自然语言指令
- 🤖 GPT 负责把指令实时翻译成 Selenium 代码并执行
- 🧠 内置"记忆"模块,能记住浏览过的页面内容并做总结
一句话:像写文档一样写自动化脚本。
二、快速开始:3步完成配置
第 1 步:安装 BrowserPilot
打开终端,执行:
pip install browserpilot要求 Python 3.10 以上,依赖项见 pyproject.toml。
第 2 步:配置 Chromedriver
- 从 Chromium 官网下载最新的Chromedriver稳定版,解压后放到你的指令文件同目录(例如项目根目录下的
./chromedriver) - 如果权限受限(Mac 上常见),右键解压出的 chromedriver → 选择"打开"解除限制,确保 Python 能调用它
第 3 步:设置 OpenAI API Key
用你习惯的方式创建环境变量:
export OPENAI_API_KEY="你的API密钥"到这里,配置就全部完成了。✅
三、5分钟跑通第一个自然语言浏览器自动化任务
1. 写指令文件
创建instructions.yaml(可参考 prompts/examples/buffalo_wikipedia.yaml 这个现成例子):
instructions: - Go to Google.com - Find all textareas. - Find the first visible textarea. - Click on the first visible textarea. - Type in "buffalo" and press enter. - Wait 2 seconds. - Find all anchor elements that link to Wikipedia. - Click on the first one. - Wait for 10 seconds.2. 运行任务
项目自带命令行入口 examples.py,一条命令即可执行:
python examples.py selenium instructions.yaml --chromedriver_path ./chromedriver浏览器会自动弹出,Google 搜索框被点击、"buffalo" 被输入、维基百科链接被打开——全程零代码。
3. 也可以像调用库一样使用
from browserpilot.agents.gpt_selenium_agent import GPTSeleniumAgent with open("instructions.yaml") as f: agent = GPTSeleniumAgent(f, "./chromedriver") agent.run()核心实现都在 browserpilot/agents/gpt_selenium_agent.py。
四、写好自然语言指令的技巧
指令写得好不好,直接决定自动化成功率。经验法则:像给 Copilot 写代码提示词,而不是聊天:
| ❌ 口语化写法 | ✅ 推荐写法 |
|---|---|
| find the search box | find all textareas |
| the login button | button which says "Log in" |
| find all the visible textareas | find all the textareas → 再写一行 find the first visible textarea |
小技巧:
- 🔍 用 HTML 元素术语:
textarea、anchor、input,而不是 "text box" - ✂️ 把复杂动作拆成多行,每行只做一件事
- ⚡ 用
BEGIN_FUNCTION func_name/END_FUNCTION包裹指令块,再用RUN_FUNCTION func_name调用,即可复用 - 💰 编译后的指令可存成 yaml(
instruction_output_file参数),避免重复调用 API 花钱
更多可抄作业的示例:prompts/examples/nytimes_click_login.yaml、prompts/examples/instagram.yaml、prompts/examples/memory_summarization.yaml。
五、项目结构速览
- 智能体核心:browserpilot/agents/gpt_selenium_agent.py —— 定义了点击、输入、滚动、截图、记忆查询等全部动作
- 指令编译器:browserpilot/agents/compilers/instruction_compiler.py —— 负责把自然语言编译成可执行动作
- 记忆模块:browserpilot/agents/memories/ —— 让智能体总结并回忆浏览过的页面
- 示例指令库:prompts/examples/
六、安全提醒
⚠️ BrowserPilot 会执行 GPT 生成的 Python 代码,官方在 README 中明确提示这并非安全惯例。请:
- 只让它在本地、可信的浏览器环境中运行
- 对重要账号操作保持警惕,指令越精确越安全
总结
BrowserPilot 让浏览器自动化从"写脆弱的代码"变成"写英文说明书"。只需pip 安装 → 放好 Chromedriver → 配好 API Key三步,你就能用自然语言指挥浏览器干活。现在就去试试你的第一条指令吧!🚀
【免费下载链接】browserpilotNatural language browser automation项目地址: https://gitcode.com/gh_mirrors/br/browserpilot
创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考