Airtest 的 Poco 框架中,offspring()
✅ offspring()
的意思是:
递归查找子孙控件,即:从当前控件开始,查找它的“子控件”+“孙控件”+“重孙控件”... 直到找到匹配的目标控件。
📌 举例说明:
假设一个控件层级结构如下:
父容器 A
├── 子容器 B
│ └── TextView(id="title")
如果你从控件 A 出发:
A.child("title")
❌ 找不到,因为 title 不是 直接子控件A.offspring("title")
✅ 可以找到,因为 title 是 A 的子孙控件
🆚 和其他方法对比
方法 | 作用 | 是否递归查找 |
---|---|---|
children() | 只查找 直接子控件 | 否 ❌ |
child("xxx") | 获取名为 "xxx" 的直接子控件 | 否 ❌ |
offspring("xxx") | 查找名为 "xxx" 的所有子孙控件 | 是 ✅ |
descendant("xxx") | (别名,和 offspring() 类似) | 是 ✅ |
✅ 实用示例
# 获取列表容器
list_view = poco("com.example:id/news_list")# 获取列表中第一个新闻项
first_item = list_view.children()[0]# 在第一个新闻项中递归查找标题文本
title = first_item.offspring("com.example:id/title").get_text()
print(f"新闻标题: {title}")
✅ 总结一句话:
offspring("xxx")
就像是从当前控件出发,递归往下找“全家族里叫 xxx 的人”,无论隔了几代都能找。