跳到主要内容

查找一个 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