描述
UI 类 提供在 AgentBay 云环境与 UI 元素交互的方法。这包括获取 UI 元素、发送按键事件、输入文本、执行手势操作以及截屏。
方法 | 说明 | 环境 | ||||
ComputerUseLinux | ComputerUseWindows | BrowserUse | MobileUse | CodeSpace | ||
| 在屏幕指定坐标点击(支持左键/中键/右键) | 不支持 | 不支持 | 不支持 | 支持 | 不支持 |
| 输入文本内容 | 不支持 | 不支持 | 不支持 | 支持 | 不支持 |
| 发送按键(支持Android平台特定按键:HOME、BACK、音量键等) | 不支持 | 不支持 | 不支持 | 支持 | 不支持 |
| 执行滑动手势操作(可设置起始坐标、结束坐标和持续时间) | 不支持 | 不支持 | 不支持 | 支持 | 不支持 |
| 列出所有根窗口及其信息(窗口ID、标题、进程ID、进程名) | 不支持 | 支持 | 不支持 | 不支持 | 不支持 |
| 获取当前活动窗口信息 | 不支持 | 支持 | 不支持 | 不支持 | 不支持 |
| 激活指定窗口 | 不支持 | 支持 | 不支持 | 不支持 | 不支持 |
| 最大化窗口 | 不支持 | 支持 | 不支持 | 不支持 | 不支持 |
| 最小化窗口 | 不支持 | 支持 | 不支持 | 不支持 | 不支持 |
| 恢复窗口到正常状态 | 不支持 | 支持 | 不支持 | 不支持 | 不支持 |
| 关闭窗口 | 不支持 | 支持 | 不支持 | 不支持 | 不支持 |
| 调整窗口大小(指定宽度和高度) | 不支持 | 支持 | 不支持 | 不支持 | 不支持 |
| 设置窗口为全屏模式 | 不支持 | 支持 | 不支持 | 不支持 | 不支持 |
| 启用/禁用专注模式 | 不支持 | 支持 | 不支持 | 不支持 | 不支持 |
| 获取设备上所有UI元素(包括非交互元素) | 不支持 | 不支持 | 不支持 | 支持 | 不支持 |
| 获取所有可点击的UI元素 | 不支持 | 不支持 | 不支持 | 支持 | 不支持 |
属性
属性名 | 说明 |
HOME | Home 按键(3) |
BACK | Back 按键(4) |
VOLUME_UP | 音量增加按键(24) |
VOLUME_DOWN | 音量减少按键(25) |
POWER | 电源按键(26) |
MENU | 菜单按键(82) |
方法
getClickableUIElements
- 获取可点击的 UI 元素
Golang
func (ui *UI) GetClickableUIElements(timeoutMs int) (*UIElementsResult, error)
参数:
timeoutMs(int):超时时间(毫秒)。若
<= 0
,默认为2000ms
。
返回值:
*UIElementsResult
:包含可点击UI元素和请求ID的结果对象。error
:若操作失败,返回错误信息。
UIElementsResult 结构体:
type UIElementsResult struct {
RequestID string // 调试用的唯一请求标识符
Elements []*UIElement // UI 元素数组
}
type UIElement struct {
Bounds string // 元素边界
ClassName string // CSS 类名
Text string // 文本内容
Type string // 元素类型
ResourceId string // 资源 ID
Index int // 元素索引
IsParent bool // 是否为父元素
Children []*UIElement // 子元素
}
Python
def get_all_ui_elements(timeout_ms: int = 2000) -> List[Dict[str, Any]]
参数:
timeout_ms(int,可选):超时时间(毫秒)。默认为
2000ms
。
返回值:
List[Dict[str, Any]]
:包含解析详情的所有UI元素列表。
异常:
AgentBayError
:若操作失败,引发错误。
TypeScript
getClickableUIElements(timeoutMs?: number): Promise<string>
参数:
timeoutMs(number,可选):超时时间(毫秒)。默认为
2000ms
。
返回值:
Promise<string>
:若成功,返回可点击UI元素的字符串表示。
异常:
APIError
:若操作失败,抛出错误。
getAllUIElements
- 获取所有 UI 元素
Golang
func (ui *UI) GetAllUIElements(timeoutMs int) (*UIElementsResult, error)
参数:
timeoutMs(int):超时时间(毫秒)。若
<= 0
,默认为2000ms
。
返回值:
*UIElementsResult
:包含所有UI元素和请求ID的结果对象。error
:若操作失败,返回错误信息。
Python
def get_all_ui_elements(timeout_ms: int = 2000) -> List[Dict[str, Any]]
参数:
timeout_ms (int, 可选): 超时时间(毫秒)。默认为
2000ms
。
返回值:
List[Dict[str, Any]]
: 包含解析详情的所有 UI 元素列表。
异常:
AgentBayError
: 若操作失败,引发错误。
TypeScript
getAllUIElements(timeoutMs?: number): Promise<string>
参数:
timeoutMs(number,可选):超时时间(毫秒)。默认为
2000ms
。
返回值:
Promise<string>
:若成功,返回所有UI元素的字符串表示。
异常:
APIError
:若操作失败,抛出错误。
sendKey
- 发送按键
Golang
func (ui *UI) SendKey(key int) (*KeyActionResult, error)
参数:
key(int):要发送的按键码。使用
KeyCode
常量。
返回值:
*KeyActionResult
:包含操作状态和请求ID的结果对象。error
:若操作失败,返回错误信息。
KeyActionResult 结构体:
type KeyActionResult struct {
RequestID string // 调试用的唯一请求标识符
Success bool // 按键是否发送成功
}
Python
def send_key(key: int) -> bool
参数:
key(int):要发送的按键码。使用
KeyCode
常量。
返回值:
bool
:若按键成功发送,返回True
。
异常:
AgentBayError
:若操作失败,引发错误。
示例:
# 发送 Home 按键
success = agent_bay.ui.send_key(KeyCode.HOME)
print("按键发送结果:", success)
TypeScript
sendKey(key: number): Promise<string>
参数:
key(number):要发送的按键码。使用
KeyCode
常量。
返回值:
Promise<string>
:若成功,返回响应文本。
异常:
APIError
:若操作失败,抛出错误。
示例:
// 发送 Home 按键
const result = await agentBay.ui.sendKey(KeyCode.HOME);
console.log("按键发送结果:", result);
inputText
- 输入文本
Golang
func (ui *UI) InputText(text string) (*TextInputResult, error)
参数:
text(string):要输入的文本。
返回值:
*TextInputResult
:包含输入文本和请求ID的结果对象。error
:若操作失败,返回错误信息。
TextInputResult 结构体:
type TextInputResult struct {
RequestID string // 调试用的唯一请求标识符
Text string // 输入的文本
}
Python
def input_text(text: str) -> None
参数:
text(str):要输入的文本。
异常:
AgentBayError
:若操作失败,引发错误。
示例:
# 输入文本 "Hello"
agent_bay.ui.input_text("Hello")
TypeScript
inputText(text: string): Promise<string>
参数:
text(string):要输入的文本。
返回值:
Promise<string>
:若成功,返回响应文本。
异常:
APIError
:若操作失败,抛出错误。
示例:
// 输入文本 "Hello"
const result = await agentBay.ui.inputText("Hello");
console.log("文本输入结果:", result);
swipe
- 执行滑动操作
Golang
func (ui *UI) Swipe(startX, startY, endX, endY, durationMs int) (*SwipeResult, error)
参数:
startX(int):起始X坐标。
startY(int):起始Y坐标。
endX(int):结束X坐标。
endY(int):结束Y坐标。
durationMs(int):滑动持续时间(毫秒)。
返回值:
*SwipeResult
:包含操作状态和请求ID的结果对象。error
:若操作失败,返回错误信息。
SwipeResult 结构体:
type SwipeResult struct {
RequestID string // 调试用的唯一请求标识符
Success bool // 滑动是否成功
}
Python
def swipe(start_x: int, start_y: int, end_x: int, end_y: int, duration_ms: int = 300) -> None
参数:
start_x(int):起始X坐标。
start_y(int):起始Y坐标。
end_x(int):结束X坐标。
end_y(int):结束Y坐标。
duration_ms(int,可选):滑动持续时间(毫秒)。默认为
300ms
。
异常:
AgentBayError
:若操作失败,引发错误。
示例:
# 从 (100, 200) 滑动到 (300, 400)
agent_bay.ui.swipe(100, 200, 300, 400)
TypeScript
swipe(
startX: number,
startY: number,
endX: number,
endY: number,
durationMs?: number
): Promise<string>
参数:
startX(number):起始X坐标。
startY(number):起始Y坐标。
endX(number):结束X坐标。
endY(number):结束Y坐标。
durationMs(number,可选): 滑动持续时间(毫秒)。默认为
300ms
。
返回值:
Promise<string>
:若成功,返回响应文本。
异常:
APIError
:若操作失败,抛出错误。
示例:
// 从 (100, 200) 滑动到 (300, 400)
const result = await agentBay.ui.swipe(100, 200, 300, 400);
console.log("滑动操作结果:", result);
click
- 点击坐标点
Golang
func (ui *UI) Click(x, y int, button string) (*UIResult, error)
参数:
x(int):X坐标。
y(int):Y坐标。
button(string):使用的鼠标按键。若为空,默认为
left
。
返回值:
*UIResult
:包含操作状态、组件ID和请求ID的结果对象。error
:若操作失败,返回错误信息。
UIResult 结构体:
type UIResult struct {
RequestID string // 调试用的唯一请求标识符
ComponentID string // 组件 ID(如适用)
Success bool // 操作是否成功
}
Python
def click(x: int, y: int, button: str = "left") -> None
参数:
x(int):X坐标。
y(int):Y坐标。
button(str,可选): 使用的鼠标按键。默认为
left
。
异常:
AgentBayError
:若操作失败,引发错误。
示例:
# 点击坐标 (200, 300) 的左键
agent_bay.ui.click(200, 300)
TypeScript
click(x: number, y: number, button?: string): Promise<string>
参数:
x(number):X坐标。
y(number):Y坐标。
button(string,可选):使用的鼠标按键。默认为
left
。
返回值:
Promise<string>
:若成功,返回响应文本。
异常:
APIError
:若操作失败,抛出错误。
示例:
// 点击坐标 (500, 800) 的左键
const result = await agentBay.ui.click(500, 800);
console.log("点击操作结果:", result);
screenshot
- 截取屏幕
Golang
func (ui *UI) Screenshot() (*UIResult, error)
返回值:
*UIResult
:包含操作状态和请求ID的结果对象。error
:若操作失败,返回错误信息。
Python
def screenshot() -> str
返回值:
str
:截屏数据。
异常:
AgentBayError
:若操作失败,引发错误。
示例:
# 截屏
image_data = agent_bay.ui.screenshot()
print("截屏数据:", image_data[:100]) # 打印前 100 字符
TypeScript
screenshot(): Promise<string>
返回值:
Promise<string>
:若成功,返回截屏数据。
异常:
APIError
:若操作失败,抛出错误。
示例:
// 截屏
const imageData = await agentBay.ui.screenshot();
console.log("截屏数据:", imageData);