跳到主要内容

文本元素模块 (ui_element)

该模块在 20260507 以后版本方可使用

ui_element 用于按文本、角色、状态或坐标读取当前界面上的 UI 元素,并对匹配到的元素执行点击、滚动、输入、切换、调节等操作。它适合脚本需要理解界面结构、减少硬编码坐标、或在不同分辨率设备上保持可读定位逻辑的场景。

固定坐标触控仍建议使用 模拟触摸模块 (touch)ui_element 中出现的坐标均为物理像素。

基本用法

local ui_element = require("ui_element")

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

if item then
ui_element.input_text(item, "hello")
else
sys.toast(err)
end

说明

查询类接口失败时通常返回 nil, errfind_all 没有匹配项时返回空数组。 动作类接口成功时返回 true, info,失败时返回 nil, err。 界面发生变化后,之前获取到的快照不再代表最新界面,应重新调用 snapshot()