let deliveryBtn = textMatches(config.keywords.delivery).depth(25).findOne(100);
提高识别速度的方法
回答
为了提高 textMatches(config.keywords.delivery).depth(25).findOne(100)
的识别速度,可以尝试以下优化方法:
1. 减少 depth
层级
depth(25)
表示搜索控件的最大层级为 25,层级越深搜索范围越大,速度越慢。- 建议:将
depth
调整为更小的值(如depth(10)
或更低),确保控件在范围内即可。
textMatches(config.keywords.delivery).depth(10).findOne(100);
2. 优化正则表达式匹配
textMatches()
使用正则表达式匹配文本,若规则过于宽泛(如/.*关键词.*/
),可能导致搜索效率降低。- 建议:使用更精确的正则表达式,或结合其他属性(如
id
、resourceId
)缩小范围。
// 示例:结合 id 和 textMatches
id("delivery_id").textMatches(/关键词/).findOne(100);
3. 使用 findOnce()
替代 findOne()
findOne(timeout)
会阻塞直到找到控件或超时,而findOnce()
只搜索一次,适合快速判断。- 建议:若无需等待,直接使用
findOnce()
,并处理可能的null
情况。
let deliveryBtn = textMatches(config.keywords.delivery).depth(10).findOnce();
if (deliveryBtn) {
// 执行操作
}
4. 缩短超时时间
findOne(100)
设置超时为 100 毫秒,若控件在短时间内无法出现,可能需调整时间。- 建议:根据实际场景合理设置超时时间,避免过长的等待。
textMatches(...).findOne(50); // 50 毫秒
5. 提前等待控件出现
- 若控件加载较慢,可先使用
waitFor()
确保控件存在后再查找。
textMatches(config.keywords.delivery).waitFor();
let deliveryBtn = textMatches(...).findOne(100);
6. 避免冗余搜索
- 若已知控件位置,可通过
className()
、resourceId()
等更精确的属性定位。
className("Button").textMatches(/关键词/).findOne(100);
优化后示例代码
// 精确匹配 + 减少层级 + 快速查找
let deliveryBtn = id("delivery_id").textMatches(/关键词/).depth(10).findOnce();
if (deliveryBtn) {
deliveryBtn.click();
} else {
log("未找到控件");
}
通过以上调整,可显著提升控件识别速度。如果仍存在问题,建议结合 设备调试
工具分析控件属性。
ℹ️ 本回答由 AI 生成,内容仅供参考