Electron App Automation
Automate any Electron desktop app using agent-browser. Electron apps are built on Chromium and expose a Chrome DevTools Protocol (CDP) port that agent-browser can connect to, enabling the same snapshot-interact workflow used for web pages.
Core Workflow
- 1. Launch the Electron app with remote debugging enabled
- Connect agent-browser to the CDP port
- Snapshot to discover interactive elements
- Interact using element refs
- Re-snapshot after navigation or state changes
CODEBLOCK0
Launching Electron Apps with CDP
Every Electron app supports the --remote-debugging-port flag since it's built into Chromium.
macOS
CODEBLOCK1
Linux
CODEBLOCK2
Windows
CODEBLOCK3
Important: If the app is already running, quit it first, then relaunch with the flag. The --remote-debugging-port flag must be present at launch time.
Connecting
CODEBLOCK4
After connect, all subsequent commands target the connected app without needing --cdp.
Tab Management
Electron apps often have multiple windows or webviews. Use tab commands to list and switch between them:
CODEBLOCK5
Webview Support
Electron <webview> elements are automatically discovered and can be controlled like regular pages. Webviews appear as separate targets in the tab list with type: "webview":
CODEBLOCK6
Note: Webview support works via raw CDP connection.
Common Patterns
Inspect and Navigate an App
CODEBLOCK7
Take Screenshots of Desktop Apps
CODEBLOCK8
Extract Data from a Desktop App
CODEBLOCK9
Fill Forms in Desktop Apps
CODEBLOCK10
Run Multiple Apps Simultaneously
Use named sessions to control multiple Electron apps at the same time:
CODEBLOCK11
Color Scheme
The default color scheme when connecting via CDP may be light. To preserve dark mode:
CODEBLOCK12
Or set it globally:
CODEBLOCK13
Troubleshooting
"Connection refused" or "Cannot connect"
- - Make sure the app was launched with INLINECODE7
- If the app was already running, quit and relaunch with the flag
- Check that the port isn't in use by another process: INLINECODE8
App launches but connect fails
- - Wait a few seconds after launch before connecting (
sleep 3) - Some apps take time to initialize their webview
Elements not appearing in snapshot
- - The app may use multiple webviews. Use
agent-browser tab to list targets and switch to the right one - Use
agent-browser snapshot -i -C to include cursor-interactive elements (divs with onclick handlers)
Cannot type in input fields
- - Try
agent-browser keyboard type "text" to type at the current focus without a selector - Some Electron apps use custom input components; use
agent-browser keyboard inserttext "text" to bypass key events
Supported Apps
Any app built on Electron works, including:
- - Communication: Slack, Discord, Microsoft Teams, Signal, Telegram Desktop
- Development: VS Code, GitHub Desktop, Postman, Insomnia
- Design: Figma, Notion, Obsidian
- Media: Spotify, Tidal
- Productivity: Todoist, Linear, 1Password
If an app is built with Electron, it supports --remote-debugging-port and can be automated with agent-browser.
Electron 应用自动化
使用 agent-browser 自动化任何 Electron 桌面应用。Electron 应用基于 Chromium 构建,并暴露一个 Chrome DevTools 协议(CDP)端口,agent-browser 可以连接到该端口,实现与网页相同的快照-交互工作流程。
核心工作流程
- 1. 启动 启用了远程调试的 Electron 应用
- 连接 agent-browser 到 CDP 端口
- 快照 以发现可交互元素
- 交互 使用元素引用
- 重新快照 在导航或状态变更后
bash
启动启用了远程调试的 Electron 应用
open -a Slack --args --remote-debugging-port=9222
连接 agent-browser 到应用
agent-browser connect 9222
从这里开始标准工作流程
agent-browser snapshot -i
agent-browser click @e5
agent-browser screenshot slack-desktop.png
使用 CDP 启动 Electron 应用
每个 Electron 应用都支持 --remote-debugging-port 标志,因为它是 Chromium 内置的功能。
macOS
bash
Slack
open -a Slack --args --remote-debugging-port=9222
VS Code
open -a Visual Studio Code --args --remote-debugging-port=9223
Discord
open -a Discord --args --remote-debugging-port=9224
Figma
open -a Figma --args --remote-debugging-port=9225
Notion
open -a Notion --args --remote-debugging-port=9226
Spotify
open -a Spotify --args --remote-debugging-port=9227
Linux
bash
slack --remote-debugging-port=9222
code --remote-debugging-port=9223
discord --remote-debugging-port=9224
Windows
bash
C:\Users\%USERNAME%\AppData\Local\slack\slack.exe --remote-debugging-port=9222
C:\Users\%USERNAME%\AppData\Local\Programs\Microsoft VS Code\Code.exe --remote-debugging-port=9223
重要提示: 如果应用已在运行,请先退出,然后使用该标志重新启动。--remote-debugging-port 标志必须在启动时存在。
连接
bash
连接到特定端口
agent-browser connect 9222
或者在每个命令上使用 --cdp
agent-browser --cdp 9222 snapshot -i
自动发现正在运行的基于 Chromium 的应用
agent-browser --auto-connect snapshot -i
执行 connect 后,所有后续命令都会针对已连接的应用,无需使用 --cdp。
标签页管理
Electron 应用通常有多个窗口或 webview。使用标签页命令列出并在它们之间切换:
bash
列出所有可用目标(窗口、webview 等)
agent-browser tab
按索引切换到特定标签页
agent-browser tab 2
按 URL 模式切换
agent-browser tab --url
settings
Webview 支持
Electron 元素会被自动发现,并且可以像普通页面一样进行控制。Webview 在标签页列表中作为独立目标出现,类型为 webview:
bash
连接到正在运行的 Electron 应用
agent-browser connect 9222
列出目标——webview 与页面一起出现
agent-browser tab
示例输出:
0: [page] Slack - 主窗口 https://app.slack.com/
1: [webview] 嵌入内容 https://example.com/widget
切换到 webview
agent-browser tab 1
正常与 webview 交互
agent-browser snapshot -i
agent-browser click @e3
agent-browser screenshot webview.png
注意: Webview 支持通过原始 CDP 连接工作。
常见模式
检查并导航应用
bash
open -a Slack --args --remote-debugging-port=9222
sleep 3 # 等待应用启动
agent-browser connect 9222
agent-browser snapshot -i
读取快照输出以识别 UI 元素
agent-browser click @e10 # 导航到某个部分
agent-browser snapshot -i # 导航后重新快照
截取桌面应用截图
bash
agent-browser connect 9222
agent-browser screenshot app-state.png
agent-browser screenshot --full full-app.png
agent-browser screenshot --annotate annotated-app.png
从桌面应用提取数据
bash
agent-browser connect 9222
agent-browser snapshot -i
agent-browser get text @e5
agent-browser snapshot --json > app-state.json
在桌面应用中填写表单
bash
agent-browser connect 9222
agent-browser snapshot -i
agent-browser fill @e3 搜索查询
agent-browser press Enter
agent-browser wait 1000
agent-browser snapshot -i
同时运行多个应用
使用命名会话同时控制多个 Electron 应用:
bash
连接到 Slack
agent-browser --session slack connect 9222
连接到 VS Code
agent-browser --session vscode connect 9223
独立与每个应用交互
agent-browser --session slack snapshot -i
agent-browser --session vscode snapshot -i
配色方案
通过 CDP 连接时的默认配色方案可能是 light。要保留深色模式:
bash
agent-browser connect 9222
agent-browser --color-scheme dark snapshot -i
或者全局设置:
bash
AGENTBROWSERCOLOR_SCHEME=dark agent-browser connect 9222
故障排除
连接被拒绝 或 无法连接
- - 确保应用已使用 --remote-debugging-port=NNNN 启动
- 如果应用已在运行,请退出并使用该标志重新启动
- 检查端口是否被其他进程占用:lsof -i :9222
应用启动但连接失败
- - 启动后等待几秒再连接(sleep 3)
- 某些应用需要时间来初始化其 webview
快照中未显示元素
- - 应用可能使用了多个 webview。使用 agent-browser tab 列出目标并切换到正确的目标
- 使用 agent-browser snapshot -i -C 包含光标交互元素(带有 onclick 处理程序的 div)
无法在输入字段中输入
- - 尝试使用 agent-browser keyboard type text 在当前焦点处输入,无需选择器
- 某些 Electron 应用使用自定义输入组件;使用 agent-browser keyboard inserttext text 绕过按键事件
支持的应用
任何基于 Electron 构建的应用都可以工作,包括:
- - 通讯: Slack、Discord、Microsoft Teams、Signal、Telegram Desktop
- 开发: VS Code、GitHub Desktop、Postman、Insomnia
- 设计: Figma、Notion、Obsidian
- 媒体: Spotify、Tidal
- 生产力: Todoist、Linear、1Password
如果应用是使用 Electron 构建的,它支持 --remote-debugging-port,并且可以使用 agent-browser 进行自动化。