控件操作
# 优化控件查找函数增强版
```JavaScript
/*
**函数不收取任何费用,觉得不错可以小红包支持一波😄谢谢
**代码编写与测试:魚離ヤ吥開氺 QQ:631206885
**测试时间:2020.02.17
**测试系统:安卓10
**Auto.js版本: 4.1.1 && Pro 8.0.0-5
**使用过程如出现bug、增加新功能等Find_Controls
可以自行修改或联系我进行改进。
//调用示例------
Find_Controls(data,click_type);
其中data为必填参数,click_type为可选填参数
click_type 可选填参数有4种值{
1 表示点击控件中心范围+小随机偏移(比1.5可能好一些);
1.5 表示 在控件范围内随机坐标点击(请确保控件在屏幕可见)
2 表示 直接点击控件
新增功能 (data为字符串或数组时忽略clickable值进行点击)
3 或>3 表示 只判断查找控件是否存在,不进行任何点击
不填click_type,则默认该参数为1
}
data 为字符串时{
如: Find_Controls("我的钱包",2);
表示查找与"通讯录"有关的控件
找到则进行控件点击(忽略clickable值)
并返回true,没有找到则返回false;
}
data 为数组时{
如:Find_Controls(["我的钱包","通讯录"],3);
表示按数组顺序查找有关有关字符串的控件
找到一个返回true,不点击
一个也没有找到则返回false;
}
cli
data 为对象时{
如: Find_Controls({
xzq:'textContains("通讯录").depth(12)'
},1);
表示查找与选择器
textContains为"通讯录",depth为12有关的控件
找到则在控件范围内随机点击
并返回true,没有找到则返回false;
}
*/
//示范
launchApp("微信");
sleep(1500);
//找到数组内元素相关的控件,并进行控件范围内随机点击
Find_Controls("通讯录");
//封装函数-----起始
function Find_Controls(data, click_type) {
click_type = click_type || 1;
let A_解析_传入类型_ = Object.prototype.toString.call(data) === '[object Object]' ? "对象" : Object.prototype.toString.call(data) === '[object Array]' ? "数组" : Object.prototype.toString.call(data) === '[object String]' ? "字符串" : false; //true)
switch (A_解析_传入类型_) {
case "对象":
log("传入参数为对象格式");
result = find_xzq(data);
result ? log("找到「" + data + "」相关控件") : log("没有找到「" + data + "」相关控件");
return result;
case "数组":
log("传入参数为数组格式");
for (let v of data) {
result = find(v);
if (result) {
//找到其中一个就退出
break;
}
}
result ? log("找到「" + data + "」相关控件") : log("没有找到「" + data + "」相关控件");
return result;
case "字符串":
log("传入参数为字符串格式");
result = find(data);
result ? log("找到「" + data + "」相关控件") : log("没有找到「" + data + "」相关控件");
return result;
case false:
toastLog("传入参数有误");
return false;
}
function find(str) {
try {
if(click_type == 1 ||click_type == 1.5 ){
visible = "visibleToUser(true).";
}else{
visible = "";
}
bounds = eval(visible + "descContains(str).findOne(1)");
bounds == null ? bounds = eval(visible + "textContains(str).findOne(1)") : 0;
try {
switch(click_type){
case 1:
click(bounds.bounds().centerX()+random(-10,10),bounds.bounds().centerY()+random(-10,10));
break;
case 1.5:
click(random(bounds.bounds().left, bounds.bounds().right), random(bounds.bounds().top, bounds.bounds().bottom));
break;
case 2:
click(bounds.desc());
break;
default:
0;
}
} catch (e) {
switch(click_type){
case 1:
click(bounds.bounds().centerX()+random(-10,10),bounds.bounds().centerY()+random(-10,10));
break;
case 1.5:
click(random(bounds.bounds().left, bounds.bounds().right), random(bounds.bounds().top, bounds.bounds().bottom));
break;
case 2:
click(bounds.text());
break;
default:
0;
}
}
if (bounds == null && click_type > 2) {
text("--qwertyuii故意出错--").findOne(1).click();
}
return true;
} catch (e) {
log(e);
return false;
}
}
function find_xzq() {
try {
let bounds = eval(data.xzq + ".findOne(1)");
switch(click_type){
case 1:
click(bounds.bounds().centerX()+random(-10,10),bounds.bounds().centerY()+random(-10,10));
break;
case 1.5:
click(random(bounds.bounds().left, bounds.bounds().right), random(bounds.bounds().top, bounds.bounds().bottom));
break;
case 2:
bounds.click();
break;
default:
0;
}
if (bounds == null && click_type > 2) {
text("--qwertyuii故意出错--").findOne(1).click();
}
return true;
} catch (e) {
return false;
}
}
}
//封装函数-----结束
```
----