如何在 Chrome 136+ 用 browser-use 打开「带登录态」的浏览器

在 Chrome 136+ 用 browser-use 打开「带登录态」浏览器的完整攻略

适用场景:Python + Playwright ⬄ browser-use ≥ 0.1.42
目标:无需重复登录,稳定复用本机 Chrome Profile

一、背景

版本 说明
Chrome ≤135 允许默认 Profile 开启调试端口 无特殊限制
Chrome ≥136 强制要求 **--user-data-dir** 若进程携带 --remote-debugging-port/pipe,必须 同时提供 非默认 --user-data-dir。默认 Profile 会屏蔽调试端口 默认 Profile 禁用调试端口
**browser-use≤0.1.40** 连接默认 Profile 时触发 **ECONNREFUSED** 无法适配新安全策略 错误:ECONNREFUSED ::1:9222
**browser-use≥0.1.42** 自动创建临时 Profile(**/tmp/browseruse-*** 登录态丢失但可正常连接

1. Chrome 136 升级日志文档

  • Custom data directory required for remote debugging

Remote debugging via a TCP port or a pipe is no longer possible in Google Chrome with the default data directory on Windows, Linux, and macOS. A custom data directory must be specified to remotely debug Google Chrome using the --user-data-dir switch, when using the --remote-debugging-pipe or --remote-debugging-port switches.

We've made this change because these remote debugging switches are being abused by infostealers and malware to extract data from Google Chrome. A custom user data directory uses a different encryption key and so it prevents malware stealing encrypted data such as cookies.

This change does not affect Chrome for Testing and Chromium.

  • Chrome 136 on Linux, macOS, Windows

2. Python 代码报错文案

ERROR [browser] ❌ Failed to start a new Chrome instance: BrowserType.connect_over_cdp: connect ECONNREFUSED ::1:9222

Call log:

retrieving websocket url from http://localhost:9222

ERROR [browser] Failed to initialize Playwright browser: To start chrome in Debug mode, you need to close all existing Chrome instances and try again otherwise we can not connect to the instance.

ERROR [browser] ❌ Failed to create new browser session: To start chrome in Debug mode, you need to close all existing Chrome instances and try again otherwise we can not connect to the instance. (did the browser process quit?)

WARNING [browser] ⚠️ Page load failed, continuing...

二、核心思路

  1. 使用 shell 脚本复制一份现有 Profile(包含 cookie 等)到专用目录
  2. Python 代码里显式设置 --user-data-dir=<专用目录>
  3. 可选:多实例/并行→每份 Profile 用独立端口

三、Shell 脚本

1. Shell 脚本

bash 复制代码
#!/usr/bin/env bash
set -e


PROFILE_DIR="${HOME}/.chrome-profiles/agent"      # 可以改成自己需要放在的位置
CHROME_DEFAULT="${HOME}/Library/Application Support/Google/Chrome/Default"


echo "▶ 准备持久化 profile:$PROFILE_DIR"
if [ -d "$PROFILE_DIR" ]; then
  echo "  目录已存在,跳过复制(如果要重新复制,请先删掉)"
else
  mkdir -p "$PROFILE_DIR"
  echo "  复制现有登录态...(可能稍慢,取决于文件大小)"
  cp -R "$CHROME_DEFAULT" "$PROFILE_DIR/Default"
  echo "  ✔ 完成"
fi

echo "ℹ️  以后在 Python 里把 --user-data-dir 指向:$PROFILE_DIR"

2. 执行

plain 复制代码
chmod +x prepare_chrome_profile.sh
./prepare_chrome_profile.sh

四、Python 示例(browser-use ≥ 0.1.42)

python 复制代码
from browser_use import Browser, BrowserConfig, Agent
from langchain_openai import ChatOpenAI
from pydantic import SecretStr
import os, asyncio

llm = ChatOpenAI(
    base_url="https://api.deepseek.com/v1",
    model="deepseek-chat",
    api_key=SecretStr("sk-...")
)

PROFILE_DIR = os.path.expanduser("~/chrome-profiles/agent")

browser = Browser(
    config=BrowserConfig(
        browser_binary_path="/Applications/Google Chrome.app/Contents/MacOS/Google Chrome",
        extra_browser_args=[
            f"--user-data-dir={PROFILE_DIR}",
            "--profile-directory=Default",
        ],
        chrome_remote_debugging_port=9223,
        headless=False,
        keep_alive=True
    )
)

async def main():
    agent = Agent(
        task="示例任务 ...",
        llm=llm,
        browser=browser,
        use_vision=False
    )
    print((await agent.run()).final_result())

asyncio.run(main())

五、兼容旧版三策略

  1. 升级 browser-use 并加载持久 Profile(推荐,上文示例)
  2. 继续 0.1.40 + 手动启动 Chrome
plain 复制代码
google-chrome --remote-debugging-port=9222 --user-data-dir=/path/to/profile
  1. 然后 BrowserConfig(cdp_url="http://localhost:9222\")
  2. 降级 Chrome ≤ 135(应急,不建议长期)

六、常见疑问 & 排错

1. 必须每次跑 .sh 吗?

复制 Profile 只做一次;重置登录态时再执行。

2. 提示 profile locked / Chrome 已在运行?

关闭所有手动 Chrome,再启脚本。

3. 多 Agent 并发?

为每个实例复制独立 Profile,并用不同 chrome_remote_debugging_port。

4. chrome_instance_path 提示弃用?

升级后统一用 browser_binary_path。

5. 还是报 ECONNREFUSED 9222?

确认已传入 --user-data-dir=<非默认目录>;端口被占时换新端口。

引用
相关推荐
熊猫_豆豆1 分钟前
Python月球、地球、太阳三天体联动一个月的月相图
python·农历·月亮
lulu12165440784 分钟前
IDEA+Claude Code智能辅助:保姆级高效开发教程
java·人工智能·intellij-idea·ai编程
小陈工7 分钟前
Python开源代码管理避坑实战:从Git高级操作到Docker环境配置
开发语言·git·python·安全·docker·开源·源代码管理
小陈工10 分钟前
2026年3月27日技术资讯洞察:量子计算密码突破、硬件安全新范式与三月网络安全警报
服务器·python·安全·web安全·单元测试·集成测试·量子计算
氦客10 分钟前
Windows 安装 Claude Code 并接入 GLM5.0 大模型,实现 CLI AI 编程
windows·ai编程·cli·ai coding·coding plan·命令行编程·glm5.0大模型
程序员老刘·11 分钟前
Flutter版本选择指南:3.41开始进入稳定区间 | 2026年3月
flutter·ai编程·跨平台开发·客户端开发
Dovis(誓平步青云)14 分钟前
《 One-KVM 的硬件级远控方案,通过 玩客云 等廉价硬件实现 视频信号采集 + 键鼠模拟 + 虚拟 USB》
运维·前端·网络·ai编程
倾一生爱恋换一世纯真18 分钟前
使用python代码生成ragas的测试文档testset.json
人工智能·python·ragas·rag测试
繁星星繁20 分钟前
Python基础语法(一)
c++·笔记·python
未来转换21 分钟前
Python 框架 Django 从入门到精通
数据库·python·django