节点检索
- Regex 正则表达式
- Seletor 选择器
- Node 节点操作对象
# Selector
创建一个选择器:
Selector()
# id
id检索:
Selector().id("re")
re 正则表达式:控件的id
- 所有控件内部均会出现id信息
- 但不是连串英文一般为系统生成的id,为了小程序兼容性一般尽量避免使用.
# text
文本检索:
Selector().text("re")
re 正则表达式:控件的文本
- button,textview,imagebutton 等控件内部会出现文本信息
# type
类型检索:
Selector().type("re")
re 正则表达式:控件的类型
- 所有控件均有类型 如 TextView,ImageView,Button,ListView...
# desc
描述检索:
Selector().desc("re")
re 正则表达式:控件的描述信息
- 部分控件会存在描述信息
# hintText
输入控件的默认信息:
Selector().hintText("re")
re 正则表达式:默认信息的文本
- 一般在输入控件(EditText)中会出现该属性
# packageName
控件的所属包名:
Selector().packageName("re")
re 正则表达式:包名检索规则
- 所有控件均有所属包名,一个页面中可能会出现不同的包名.如状态栏的控件为系统包名.主屏幕显示当前打开应用的包名.
- 包名是应用的唯一ID
# childCount
拥有的孩子控件数量:
#语法
Selector().childCount(i...)
#案例
Selector().childCount(0.2) #孩子控件个数0-2之间皆满足(包含0-2)
Selector().childCount(2,3,10) #孩子控件个数为 2,3,10的都满足
2
3
4
5
6
7
8
i number多参类型:孩子控件的数量限定,支持多参,支持小数
- 应用页面布局都呈树状结构,一个控件可能包含多个孩子节点.
# inputType
控件的输入模式:
Selector().inputType(i)
i number类型:输入控件的类型值
- 一般在输入控件中会存在输入模式,密码模式,文本模式,数字模式,皆有不同的数值.可结合控件树工具.使用场景一般为区分 账号输入框,密码输入框等.
# maxTextLength
控件文本的长度限制:
Selector().maxTextLength(i...)
i... number多参类型:文本输入的最大长度限制,,支持小数
- 在应用页面布局中,有些文本会超出屏幕,影响整体布局的美观.因此程序员会通过maxTestLength控制文本的最大长度.
# clickable
是否可点击:
Selector().clickable(b)
b 布尔类型:True或False
- 限定要找的控件是否可点击
# checkable
是否可选中:
Selector().checkable(b)
b 布尔类型:True或False
- 一般指单选控件 和多选控件 是否可以选中
# checked
是否选中了:
Selector().checked(b)
b 布尔类型:True或False
- 单选控件 和多选控件 的选中状态
# editable
是否可编辑:
Selector().editable(b)
b 布尔类型:True或False
- 可以输入文本的控件
# enabled
是否可访问:
Selector().enabled(b)
b 布尔类型:True或False
- 控件是否可以被访问
# dismissable
是否可以被关闭:
Selector().dismissable(b)
b 布尔类型:True或False
- 控件是否可以被关闭,隐藏等
# focusable
是否可以获得焦点:
Selector().focusable(b)
b 布尔类型:True或False
- 一个页面中,部分控件可以获取焦点,部分控件不可以获取焦点
# focused
是否获得了焦点:
Selector().focused(b)
b 布尔类型:True或False
- 一个页面中,某一个时刻.只能有一个控件获取焦点,比如当我们点击输入框,则输入框就获取了焦点.
# longClickable
是否可以长按:
Selector().longClickable(b)
b 布尔类型:True或False
- 部分控件可以支持长按,比如长按复制,长按删除.
# getParent
获取父控件
Selector().getParent(i...)
Selector().getParent(2) #获取爷爷控件(父亲的父亲是爷爷)
Selector().getParent(1.2) #获取1-2 个父控件
Selector().getParent(1.2,4) #获取1-2 个父控件 和第4个父控件
2
3
4
5
6
7
8
i number类型,可变参数,选填:获取第几个父控件,默认为1
- 1个控件会存在父控件,爷爷控件,太爷爷控件....
# getParents
获取所有父亲控件
Selector().getParents()
无参数
- 该方法是指 获取父控件,爷爷控件,太爷爷控件...所有祖辈控件全部获取
# getChild
获取子控件
Selector().getChild(i)
Selector().getChild(2) #获取第2个子控件
Selector().getChild(1.3) #获取1-3个子控件
Selector().getChild(1.2,5) #获取1-2哥子控件 和第5个子控件
Selector().getChild(-2) #获取倒数第2个子控件
Selector().getChild(-1.2) #获取倒数1-2哥子控件
2
3
4
5
6
7
8
9
10
11
12
i number类型,可变参数,选填:获取第几个子控件,默认为1
- 该方法是指 获取父控件,爷爷控件,太爷爷控件...所有祖辈控件全部获取
# getChildren
获取所有子控件
Selector().getChildren()
无参数
- 如果该控件有子控件,将获取所有
# getBrother
获取子控件
Selector().getBrother(i)
Selector().getBrother(2) #获取第2个兄弟控件
Selector().getBrother(1.3) #获取1-3个兄弟控件
Selector().getBrother(1.2,5) #获取1-2个兄弟控件 和第5个兄弟控件
Selector().getBrother(-2) #获取倒数第2个兄弟控件
Selector().getBrother(-1.2) #获取倒数1-2个兄弟控件
Selector().getBrother(-0.1) #获取当前控件 的上一个兄弟控件
Selector().getBrother(0.1) #获取当前控件 的下一个兄弟控件
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
i number类型,可变参数,选填:获取第几个子控件,默认为1
- 注意,这里的参数用法:
- (正整数=获取第n个兄弟控件,负整数=倒叙)
- (正小数=获取小树整数位到小数位所有的兄弟控件,负小数=倒叙)
- (整数位=0 正小数代表:当前控件向下偏移n位的兄弟控件,负小数代表:向上偏移)
# getBrothers
获取所有兄弟控件
Selector().getBrothers()
无参数
- 兄弟控件:同一个父亲下的所有同级控件
# click
Selector 中也支持点击命令,方便对一批满足条件的控件执行点击,但返回结果仍然是Selector.
Selector().click()
无参数
- 此 点击 无反馈(是否点击成功) 如需反馈,请使用 Node的click方法
# slide
Selector 中也支持控件滑动指令,控件的滑动指令 只支持前后两个方向滑动,无法指定距离.
- i number类型,可变参数:滑动i步 ,当i大于0向前滑动, 当i小于0向后滑动
Selector().slide(i)
Selector().slide(2) # 向前滑动两步
Selector().slide(-2) # 向后滑动两步
2
3
4
5
6
此 滑动没有成功反馈,如需反馈,请使用Node.slide方法
# input
Selector 中也支持文本输入指令 s String类型:要输入的文本内容
Selector().input(s)
- 此 输入 无成功反馈,如需反馈,请使用 Node的input方法
# find
执行查询
Selector()...find()
return: Node[] 返回Node数组(控件数组)
- 当所有检索规则写完以后,记得要执行find方法来执行,得到Node(控件)
# Node
Node 代指一个控件,节点. 是Selector 规则执行find后的产物
# 属性
Node 为一个控件,控件有很多属性,可供开发者使用.
nodes = Selector().type("ImageView").find()
if len(nodes)>0:
node = nodes[0]; #获取第一个查找到的控件
print(node.id) #获取控件id
print(node.text) #获取控件文本内容
print(node.desc) #获取控件描述信息
print(node.type) #获取控件类型
print(node.packageName) #获取控件所属包名
print(node.hintText) #获取控件默认展示文本,一般在EditText控件中存在
print(node.childCount) #获取孩子控件的总数量
print(node.inputType) #获取文本输入类型
print(node.maxTextLength) #获取孩子控件的总数量
print(node.clickable) #是否可点击
print(node.longClickable) #是否可长按
print(node.checked) #是否选中
print(node.checkable) #是否可选中
print(node.editable) #是否可编辑
print(node.dismissable) #是否可消失
print(node.focusable) #是否可获取焦点
print(node.focused) #是否已经获取了焦点
#node.rect:控件相对于屏幕的坐标,分(左上),(右下),2个坐标
print(f"{node.rect.left},{node.rect.top},{node.rect.right},{node.rect.bottom}")
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# find
Node对象也可以执行find方法来继续查询
- Selector() 筛选器:要在node基础上再去查找控件的规则
nodes = Selector()...find()
if len(nodes)>0:
node = nodes[0]
node.find(Selector()...) # 这里的Selector查询是在node 的基础上继续查询
2
3
4
return: Node[] 返回Node数组(控件数组)
当所有检索规则写完以后,记得要执行find方法来执行,得到Node(控件)
# child
# children
- 无参数
nodes = Selector().id("com.aojoy.pointtest:id/search_query_section").find()
print(len(nodes))
if len(nodes)>0:
node = nodes[0]; #获取第一个查找到的控件
childs = node.children() #获取node的所有孩子控件
print(childs)
2
3
4
5
6
return: Node[] 返回Node[]控件数组
# parent
Node对象也可以获取自己的父控件
- i... number 选填 可变参数:要获取第几个父控件,默认为1
nodes = Selector()...find()
if len(nodes)>0:
node = nodes[0]
node.parent(i...) # 这里的Selector查询是在node 的基础上继续查询
node.parent() #获取父控件
node.parent(2) #获取爷爷控件(父亲的父亲是爷爷)
node.parent(1.2) #获取1-2 个父控件
node.parent(1.2,4) #获取1-2 个父控件 和第4个父控件
2
3
4
5
6
7
8
9
10
11
12
13
return: Node[] 返回Node[]控件数组
# getParents
获取所有父控件
- 无参数
nodes = Selector()...find()
if len(nodes)>0:
node = nodes[0]
node.getParents() #获取所有父控件
2
3
4
return: Node[] 返回Node[]控件数组
# getBrother
获取兄弟控件
- i... number可变参数 获取哪个兄弟节点 默认为1
nodes = Selector().id("com.aojoy.pointtest:id/search_query_section").getChild().find()
if len(nodes)>0:
node = nodes[0]; #获取第一个查找到的控件
node.getBrother(i...)
node.getBrother(2) #获取第2个兄弟控件
node.getBrother(1.3) #获取1-3个兄弟控件
node.getBrother(1.2,5) #获取1-2个兄弟控件 和第5个兄弟控件
node.getBrother(-2) #获取倒数第2个兄弟控件
node.getBrother(-1.2) #获取倒数1-2个兄弟控件
node.getBrother(-0.1) #获取当前控件 的上一个兄弟控件
node.getBrother(0.1) #获取当前控件 的下一个兄弟控件
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
return: Node[] 返回Node[]控件数组
# getBrothers
获取所有兄弟控件
- 无参数
nodes = Selector().id("com.aojoy.pointtest:id/search_query_section").getChild().find()
if len(nodes)>0:
node = nodes[0]; #获取第一个查找到的控件
node.getBrothers() #获取所有的兄弟控件
2
3
4
兄弟控件:同一个父亲下的所有同级控件
# click
对Node控件进行点击
nodes = Selector().id("com.aojoy.pointtest:id/search_query_section").find()
if len(nodes)>0:
node = nodes[0]; #获取第一个查找到的控件
node.click() #点击控件
2
3
4
- return boolean返回一个布尔值,True为点击成功,False为点击失败
# slide
对Node控件进行滑动
- i number类型,可变参数:滑动i步 ,当i大于0向前滑动, 当i小于0向后滑动
nodes = Selector().id("com.aojoy.pointtest:id/search_query_section").find()
if len(nodes)>0:
node = nodes[0]; #获取第一个查找到的控件
node.slide(i) #滑动控件语法
node.slide(1) # 向前滑动一步
node.slide(2) # 向前滑动两步
node.slide(-2) # 向后滑动两步
2
3
4
5
6
7
8
9
10
11
12
- return boolean返回一个布尔值,True为滑动成功,False为滑动失败
# input
Node 支持文本输入指令
- s string类型:要输入的文本内容
nodes = Selector().id("com.aojoy.pointtest:id/search_query_section").find()
if len(nodes)>0:
node = nodes[0]; #获取第一个查找到的控件
node.input(s) #要输入的文本
2
3
4
- return boolean返回一个布尔值,True为输入成功,False为输入失败
# Regex
# 包含匹配
题目标字符串:
"我是自在老师"
检索其中是否包含自在老师:
('自在老师')
2
3
4
5