描述
UI 類 提供在 AgentBay 雲環境與 UI 元素互動的方法。這包括擷取 UI 元素、發送按鍵事件、輸入文本、執行手勢操作以及截屏。
方法 | 說明 | 環境 | ||||
ComputerUseLinux | ComputerUseWindows | BrowserUse | MobileUse | CodeSpace | ||
| 在螢幕指定座標點擊(支援左鍵/中鍵/右鍵) | 不支援 | 不支援 | 不支援 | 支援 | 不支援 |
| 輸入常值內容 | 不支援 | 不支援 | 不支援 | 支援 | 不支援 |
| 發送按鍵(支援Android平台特定按鍵:HOME、BACK、音量鍵等) | 不支援 | 不支援 | 不支援 | 支援 | 不支援 |
| 執行滑動手勢操作(可設定起始座標、結束座標和期間) | 不支援 | 不支援 | 不支援 | 支援 | 不支援 |
| 截取會話中的畫面,並儲存到指定的OSS地址 | 支援 | 支援 | 支援 | 支援 | 不支援 |
| 列出所有根視窗及其資訊(視窗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);