跳至主要內容

查找一個 UI 元素 (ui_element.find)

該函式在 20260507 以後版本方可使用

聲明

元素, 錯誤資訊 = ui_element.find(選擇器 [, 選項 ])

參數

回傳值

  • 元素 表型,第一個匹配的 UI 元素。失敗時為 nil
  • 錯誤資訊 文字型,失敗原因。沒有匹配項時通常為 "target not found"

說明

如果選擇器中包含 index,則返回匹配結果中的第 index 個元素。 建議使用 titletext_contains 搭配 role 定位,減少同名元素帶來的歧義。

範例

local ui_element = require("ui_element")

local item, err = ui_element.find({
title = "搜索",
role = "text_field",
})

if item then
ui_element.click(item)
else
sys.toast(err)
end

:上述代碼中使用了非本章函式 sys.toast