基于大语言模型的浏览器自动化:playwright+browser-use
1、安装nodejs
https://nodejs.org/zh-cn/download 下载 node-v24.11.0-x64.msi
2、安装 playwright
npx playwright install



3、安装 browser-use 包
首先需要安装了python环境,然后创建 python 虚拟环境

创建 python 虚拟环境,也可以使用
conda create --name browser_use python=3.11
conda activate browser_use
安装 browser-use 包
pip install browser-use -i https://pypi.tuna.tsinghua.edu.cn/simple
4、根据使用的大模型服务,在环境变量设置里配置相应的 API key
OPENAI_API_KEY(gpt-4o 模型)
ANTHROPIC_API_KEY(claude 模型)
GEMINI_API_KEY(gemini 模型)
DEEPSEEK_API_KEY(deepseek 的 r1、v3 模型)
5、示例1:浏览器搜索,回答用户问题
需要用到的依赖模块
langchain-openai:提供ChatOpenAI类,用于与OpenAI的聊天模型进行交互
browser-use:包含Agent类,实现智能浏览器自动化功能
asyncio:Python标准库,用于异步编程,无需额外安装
os:Python标准库,提供操作系统接口,无需额外安装
pydantic:提供SecretStr等数据验证功能,用于安全处理敏感信息
安装依赖
pip install langchain-openai pydantic -i https://pypi.tuna.tsinghua.edu.cn/simple
安装browser-use环境
参考:https://github.com/browser-use/browser-use?tab=readme-ov-file
1. Create environment with uv (Python>=3.11):
uv init
2. Install Browser-Use package:
# We ship every day - use the latest version! uv add browser-use uv sync
执行结果:
PS C:\Users\EDY> uv init Initialized project `edy` PS C:\Users\EDY> uv sync >> uv add browser-use >> # We ship every day - use the latest version! Using CPython 3.13.2 interpreter at: Desktop\work\installtools\python-3.13.2-amd64\python.exe Creating virtual environment at: .venv Resolved 1 package in 3ms Audited in 0.02ms Resolved 261 packages in 3.31s Prepared 97 packages in 10.93s Installed 97 packages in 433ms + aiofiles==25.1.0 + aiohappyeyeballs==2.6.1 + aiohttp==3.12.15 + aiosignal==1.4.0 + annotated-types==0.7.0 + anthropic==0.72.0 + anyio==4.11.0 + attrs==25.4.0 + authlib==1.6.5 + backoff==2.2.1 + beautifulsoup4==4.14.2 + browser-use==0.9.4 + bubus==1.5.6 + cachetools==6.2.1 + cdp-use==1.4.3 + certifi==2025.10.5 + cffi==2.0.0 + charset-normalizer==3.4.4 + click==8.3.0 + colorama==0.4.6 + cryptography==46.0.3 + distro==1.9.0 + docstring-parser==0.17.0 + frozenlist==1.8.0 + google-api-core==2.28.1 + google-api-python-client==2.185.0 + google-auth==2.42.0 + google-auth-httplib2==0.2.0 + google-auth-oauthlib==1.2.2 + google-genai==1.46.0 + googleapis-common-protos==1.71.0 + groq==0.33.0 + h11==0.16.0 + httpcore==1.0.9 + httplib2==0.31.0 + httpx==0.28.1 + httpx-sse==0.4.3 + idna==3.11 + inquirerpy==0.3.4 + jiter==0.11.1 + jsonschema==4.25.1 + jsonschema-specifications==2025.9.1 + markdown-it-py==4.0.0 + markdownify==1.2.0 + mcp==1.19.0 + mdurl==0.1.2 + multidict==6.7.0 + oauthlib==3.3.1 + ollama==0.6.0 + openai==1.109.1 + pfzy==0.3.4 + pillow==12.0.0 + portalocker==2.10.1 + posthog==6.7.11 + prompt-toolkit==3.0.52 + propcache==0.4.1 + proto-plus==1.26.1 + protobuf==6.33.0 + psutil==7.1.2 + pyasn1==0.6.1 + pyasn1-modules==0.4.2 + pycparser==2.23 + pydantic==2.12.3 + pydantic-core==2.41.4 + pydantic-settings==2.11.0 + pygments==2.19.2 + pyotp==2.9.0 + pyparsing==3.2.5 + pypdf==6.1.3 + python-dateutil==2.9.0.post0 + python-dotenv==1.2.1 + python-multipart==0.0.20 + pywin32==311 + referencing==0.37.0 + reportlab==4.4.4 + requests==2.32.5 + requests-oauthlib==2.0.0 + rich==14.2.0 + rpds-py==0.28.0 + rsa==4.9.1 + screeninfo==0.8.1 + six==1.17.0 + sniffio==1.3.1 + soupsieve==2.8 + sse-starlette==3.0.2 + starlette==0.49.1 + tenacity==9.1.2 + tqdm==4.67.1 + typing-extensions==4.15.0 + typing-inspection==0.4.2 + uritemplate==4.2.0 + urllib3==2.5.0 + uuid7==0.1.0 + uvicorn==0.38.0 + wcwidth==0.2.14 + websockets==15.0.1 + yarl==1.22.0 PS C:\Users\EDY>
3. Get your API key from Browser Use Cloud and add it to your .env file (new signups get $10 free credits):
# .env BROWSER_USE_API_KEY=your-key
4. Install Chromium browser:
uvx browser-use install
执行结果:

5. Run your first agent:
from browser_use import Agent, Browser, ChatBrowserUse import asyncio async def example(): browser = Browser( # use_cloud=True, # Uncomment to use a stealth browser on Browser Use Cloud ) llm = ChatBrowserUse() agent = Agent( task="Find the number of stars of the browser-use repo", llm=llm, browser=browser, ) history = await agent.run() return history if __name__ == "__main__": history = asyncio.run(example())
posted on 2025-10-29 17:16 wenbin_ouyang 阅读(6) 评论(0) 收藏 举报
浙公网安备 33010602011771号