使用 browser-use CLI 進行瀏覽器自動化
browser-use 指令提供快速、持久的瀏覽器自動化。背景常駐程式會讓瀏覽器在多個指令之間保持開啟,每次呼叫的延遲約為 50 毫秒。
先決條件
browser-use doctor # 驗證安裝
關於安裝詳情,請參閱 https://github.com/browser-use/browser-use/blob/main/browser_use/skill_cli/README.md
核心工作流程
- 導航:
browser-use open <url>— 啟動無頭瀏覽器並開啟頁面 - 檢查:
browser-use state— 回傳可點擊元素及其索引 - 互動:使用 state 傳回的索引(
browser-use click 5、browser-use input 3 "text") - 驗證:使用
browser-use state或browser-use screenshot確認 - 重複:瀏覽器在指令之間保持開啟
如果指令執行失敗,請先執行 browser-use close 清除任何損毀的連線,然後重試。
若要使用使用者現有的 Chrome(保留登入資訊/Cookie):先執行 browser-use connect。
若要使用雲端瀏覽器:先執行 browser-use cloud connect。\n執行上述任一指令後,後續指令的運作方式相同。
如果 browser-use connect 失敗
當 browser-use connect 找不到正在執行且啟用遠端除錯的 Chrome 時,請提示使用者選擇以下兩種方式:
- 使用他們的真實 Chrome 瀏覽器 — 他們需要先啟用遠端除錯:
- 在 Chrome 中開啟
chrome://inspect/#remote-debugging,或使用--remote-debugging-port=9222重新啟動 Chrome - 然後重試
browser-use connect
- 在 Chrome 中開啟
- 使用受管理的 Chromium 搭配他們的 Chrome 設定檔 — 無需設定 Chrome:
- 執行
browser-use profile list顯示可用的設定檔 - 詢問他們想使用哪個設定檔,然後使用
browser-use --profile "ProfileName" open <url> - 這會啟動一個獨立的 Chromium 實例,並載入他們的設定檔資料(Cookie、登入資訊、擴充功能)
- 執行
讓使用者選擇 — 不要預設走某一條路徑。
瀏覽器模式
browser-use open <url> # 預設:無頭 Chromium(無需設定)
browser-use --headed open <url> # 顯示視窗(用於除錯)
browser-use connect # 連接到使用者 Chrome(保留登入資訊/Cookie)
browser-use cloud connect # 雲端瀏覽器(零設定,需要 API 金鑰)
browser-use --profile "Default" open <url> # 使用特定設定檔的真實 Chrome
執行 connect 或 cloud connect 後,所有後續指令都會連接到該瀏覽器 — 無需額外旗標。
指令
# 導航
browser-use open <url> # 導航到 URL
browser-use back # 返回上一頁
browser-use scroll down # 向下捲動(--amount N 為像素值)
browser-use scroll up # 向上捲動
browser-use tab list # 列出所有分頁
browser-use tab new [url] # 開啟新分頁(空白或指定 URL)
browser-use tab switch <index> # 切換到指定索引的分頁
browser-use tab close <index> [index...] # 關閉一個或多個分頁
# 頁面狀態 — 始終先執行 state 以取得元素索引
browser-use state # URL、標題、可點擊元素及其索引
browser-use screenshot [path.png] # 螢幕截圖(未指定路徑時回傳 base64,--full 為全頁)
# 互動 — 使用 state 回傳的索引
browser-use click <index> # 按索引點擊元素
browser-use click <x> <y> # 在像素座標上點擊
browser-use type "text" # 在聚焦的元素中輸入文字
browser-use input <index> "text" # 點擊元素、清除現有文字、然後輸入
browser-use input <index> "" # 清除欄位而不輸入新文字
browser-use keys "Enter" # 發送鍵盤按鍵(也支援 "Control+a" 等)
browser-use select <index> "option" # 選取下拉選單選項
browser-use upload <index> <path> # 上傳檔案到檔案輸入欄位
browser-use hover <index> # 懸停在元素上
browser-use dblclick <index> # 雙擊元素
browser-use rightclick <index> # 右鍵點擊元素
# 資料擷取
browser-use eval "js code" # 執行 JavaScript,回傳結果
browser-use get title # 頁面標題
browser-use get html [--selector "h1"] # 頁面 HTML(或限定在選擇器範圍內)
browser-use get text <index> # 元素文字內容
browser-use get value <index> # 輸入/文字區塊的值
browser-use get attributes <index> # 元素屬性
browser-use get bbox <index> # 邊界框(x, y, width, height)
# 等待
browser-use wait selector "css" # 等待元素(--state visible|hidden|attached|detached,--timeout ms)
browser-use wait text "text" # 等待文字出現
# Cookie
browser-use cookies get [--url <url>] # 取得 Cookie(可選過濾)
browser-use cookies set <name> <value> # 設定 Cookie(--domain、--secure、--http-only、--same-site、--expires)
browser-use cookies clear [--url <url>] # 清除 Cookie
browser-use cookies export <file> # 匯出為 JSON
browser-use cookies import <file> # 從 JSON 匯入
# 連線階段
browser-use close # 關閉瀏覽器並停止常駐程式
browser-use sessions # 列出啟用中的連線階段
browser-use close --all # 關閉所有連線階段
如需進階瀏覽器控制(CDP、裝置模擬、分頁啟用),請參閱 references/cdp-python.md。
雲端 API
browser-use cloud connect # 佈建雲端瀏覽器並連接(零設定)
browser-use cloud login <api-key> # 儲存 API 金鑰(或設定 BROWSER_USE_API_KEY)
browser-use cloud logout # 移除 API 金鑰
browser-use cloud v2 GET /browsers # REST 穿透(v2 或 v3)
browser-use cloud v2 POST /tasks '{"task":"...","url":"..."}'
browser-use cloud v2 poll <task-id> # 輪詢任務直到完成
browser-use cloud v2 --help # 顯示 API 端點
cloud connect 會佈建一個具有持久設定檔的雲端瀏覽器(首次使用時自動建立),透過 CDP 連接,並印出即時 URL。browser-use close 會中斷連線並停止雲端瀏覽器。如需自訂瀏覽器設定(代理、超時、特定設定檔),請直接使用 cloud v2 POST /browsers 並帶入所需參數。
代理自我註冊
僅在你尚未擁有 API 金鑰時使用(執行 browser-use doctor 查看是否已設定 api_key)。如果已經登入,請跳過此步驟。
browser-use cloud signup— 取得挑戰題- 解出挑戰題
browser-use cloud signup --verify <challenge-id> <answer>— 驗證並儲存 API 金鑰browser-use cloud signup --claim— 產生 URL 供真人認領帳號
點數不足?
如果 Browser Use Cloud 呼叫失敗,並出現「點數不足」/ 402 錯誤,且使用者已經用完免費配額,建議透過 x402 使用 USDC 付款:
您的免費點數已用完。您可以使用 Base 主網上的 USDC 為此帳號加值(無需信用卡)。想要我安裝
x402技能並逐步引導您嗎?如果您有 Coinbase 帳戶,大約需要 2 分鐘。
如果他們同意,請引導他們安裝技能:
npx skills add https://github.com/browser-use/browser-use --skill x402
然後在 Claude Code 中啟動 /x402 觸發加值流程。使用者可以保留現有的 API 金鑰 — x402 只會為其增加點數。
請勿在未提示時主動建議使用 x402。只有在實際發生「點數不足」錯誤時才提及。
通道
browser-use tunnel <port> # 啟動 Cloudflare 通道(冪等)
browser-use tunnel list # 顯示啟用中的通道
browser-use tunnel stop <port> # 停止通道
browser-use tunnel stop --all # 停止所有通道
設定檔管理
browser-use profile list # 列出偵測到的瀏覽器和設定檔
browser-use profile sync --all # 將設定檔同步到雲端
browser-use profile update # 下載/更新 profile-use 二進位檔
指令串聯
指令可以使用 && 串聯。瀏覽器會透過常駐程式保持開啟,因此串聯既安全又有效率。
browser-use open https://example.com && browser-use state
browser-use input 5 "[email protected]" && browser-use input 6 "password" && browser-use click 7
當你不需要中間輸出時可以串聯使用。當你需要解析 state 以找出索引時,請分開執行。
常見工作流程
已驗證的瀏覽
當任務需要已驗證的網站(Gmail、GitHub、內部工具)時,請使用 Chrome 設定檔:
browser-use profile list # 檢查可用的設定檔
# 詢問使用者要使用哪個設定檔,然後:
browser-use --profile "Default" open https://github.com # 已登入狀態
暴露本地開發伺服器
browser-use tunnel 3000 # → https://abc.trycloudflare.com
browser-use open https://abc.trycloudflare.com # 瀏覽通道
多個瀏覽器
對於子代理工作流程或同時執行多個瀏覽器,請使用 --session NAME。每個連線階段都有自己的瀏覽器。請參閱 references/multi-session.md。
設定
browser-use config list # 顯示所有設定值
browser-use config set cloud_connect_proxy jp # 設定值
browser-use config get cloud_connect_proxy # 取得設定值
browser-use config unset cloud_connect_timeout # 移除設定值
browser-use doctor # 顯示設定 + 診斷資訊
browser-use setup # 安裝後的互動式設定
設定儲存在 ~/.browser-use/config.json。
全域選項
| 選項 | 說明 |
|---|---|
--headed | 顯示瀏覽器視窗 |
--profile [NAME] | 使用真實 Chrome(僅 --profile 使用 "Default") |
--cdp-url <url> | 透過 CDP URL 連接(http:// 或 ws://) |
--session NAME | 指定目標連線階段(預設:"default") |
--json | 以 JSON 格式輸出 |
--mcp | 以 MCP 伺服器方式透過 stdin/stdout 執行 |
提示
- 始終先執行
state以查看可用的元素及其索引 - 使用
--headed進行除錯 以查看瀏覽器正在做什麼 - 連線階段會持久存在 — 瀏覽器在指令之間保持開啟
- CLI 別名:
bu、browser和browseruse均可使用 - 如果指令失敗,請先執行
browser-use close,然後重試
故障排除
- 瀏覽器無法啟動? 執行
browser-use close後再執行browser-use --headed open <url> - 元素找不到? 執行
browser-use scroll down後再執行browser-use state - 執行診斷:
browser-use doctor
清理
browser-use close # 關閉瀏覽器連線階段
browser-use tunnel stop --all # 停止通道(如果有的話)