Browser File Upload
Automates file uploads to web pages using agent-browser CLI.
Quick Start
Upload a file to a web page:
CODEBLOCK0
Usage Patterns
Upload to Specific Selector
When you know the file input selector:
CODEBLOCK1
Upload by Text Search
When file input has visible label text:
CODEBLOCK2
Using Python Script
For reusable upload logic with flexible path specification:
CODEBLOCK3
Arguments:
- -
url - Target page URL - INLINECODE1 - Path to file (supports multiple formats, see below)
- INLINECODE2 - Optional CSS selector for file input
- INLINECODE3 - Optional wait time after page load (default: 2000)
File Path Formats
The Python script supports multiple path formats:
| Format | Example | Description |
|---|
| Absolute | INLINECODE4 | Full path |
| Relative |
./data/file.xlsx | Relative to current directory |
| Workspace |
workspace/file.xlsx | Relative to OPENCLAW_WORKSPACE |
| Env Var |
${HOME}/file.xlsx | Environment variable |
| Windows Env |
%USERPROFILE%\file.xlsx | Windows environment variable |
Path Examples
CODEBLOCK4
Common Selectors
| Selector | Description |
|---|
| INLINECODE9 | Common id for file inputs |
| INLINECODE10 |
Any file input element |
|
input[type=file] | Explicit file input |
|
.upload-zone | Drop zone containers |
Troubleshooting
File input not found:
- - Run
agent-browser snapshot to inspect page elements - Look for
[type=file] or file-related buttons - Try clicking the upload zone first
Upload fails:
- - Ensure file path is absolute
- Check file exists: INLINECODE15
- Try
agent-browser find text 选择文件 click first
Page not loaded:
- - Add
agent-browser wait 2000 after open - Use
agent-browser wait --load networkidle for slow pages
Related Commands
CODEBLOCK5
Example: Upload Excel to UU Tool
CODEBLOCK6
浏览器文件上传
使用 agent-browser CLI 自动化文件上传到网页。
快速开始
上传文件到网页:
bash
基础上传(自动检测文件输入)
agent-browser open
agent-browser find text 选择文件 click
agent-browser upload #filePicker
或使用 Python 脚本
python scripts/upload_file.py
使用模式
上传到特定选择器
当你知道文件输入选择器时:
bash
agent-browser open https://example.com/upload
agent-browser click #fileInput
agent-browser upload #fileInput C:\path\to\file.xlsx
通过文本搜索上传
当文件输入有可见标签文本时:
bash
agent-browser open https://example.com/upload
agent-browser find text 上传文件 click
agent-browser upload [type=file] C:\path\to\file.xlsx
使用 Python 脚本
用于可复用的上传逻辑,支持灵活路径指定:
bash
python scripts/uploadfile.py [selector] [waitms]
参数:
- - url - 目标页面 URL
- file-path - 文件路径(支持多种格式,见下文)
- selector - 可选的文件输入 CSS 选择器
- wait_ms - 页面加载后的可选等待时间(默认:2000)
文件路径格式
Python 脚本支持多种路径格式:
| 格式 | 示例 | 描述 |
|---|
| 绝对路径 | C:\Users\name\file.xlsx | 完整路径 |
| 相对路径 |
./data/file.xlsx | 相对于当前目录 |
| 工作空间 | workspace/file.xlsx | 相对于 OPENCLAW_WORKSPACE |
| 环境变量 | ${HOME}/file.xlsx | 环境变量 |
| Windows 环境变量 | %USERPROFILE%\file.xlsx | Windows 环境变量 |
路径示例
bash
绝对路径
python scripts/upload_file.py https://example.com C:\Users\陈\Documents\data.xlsx
相对路径(从工作空间)
python scripts/upload_file.py https://example.com ./test.xlsx
工作空间相对路径
python scripts/upload_file.py https://example.com workspace/data/test.xlsx
使用环境变量
python scripts/upload_file.py https://example.com ${HOME}/downloads/file.xlsx
常用选择器
| 选择器 | 描述 |
|---|
| #filePicker | 文件输入的常见 ID |
| [type=file] |
任何文件输入元素 |
| input[type=file] | 显式文件输入 |
| .upload-zone | 拖放区域容器 |
故障排除
找不到文件输入:
- - 运行 agent-browser snapshot 检查页面元素
- 查找 [type=file] 或与文件相关的按钮
- 尝试先点击上传区域
上传失败:
- - 确保文件路径是绝对路径
- 检查文件是否存在:test-path
- 先尝试 agent-browser find text 选择文件 click
页面未加载:
- - 在 open 后添加 agent-browser wait 2000
- 对慢速页面使用 agent-browser wait --load networkidle
相关命令
bash
检查页面元素
agent-browser snapshot
截图
agent-browser screenshot
通过文本查找元素
agent-browser find text 上传
点击元素
agent-browser click
上传文件
agent-browser upload
示例:上传 Excel 到 UU 工具
bash
方法 1:直接使用 agent-browser 命令
agent-browser open https://uutool.cn/excel/
agent-browser wait 2000
agent-browser find text 选择文件 click
agent-browser upload #filePicker C:\Users\陈\.openclaw\workspace\test.xlsx
方法 2:使用 Python 脚本和绝对路径
python scripts/upload_file.py https://uutool.cn/excel/ C:\Users\陈\.openclaw\workspace\test.xlsx
方法 3:使用 Python 脚本和工作空间相对路径
python scripts/upload_file.py https://uutool.cn/excel/ workspace/test.xlsx
方法 4:使用 Python 脚本和自定义选择器及等待时间
python scripts/upload_file.py https://uutool.cn/excel/ workspace/test.xlsx #filePicker 3000