var timeSlots = [
"8:00-9:00", "9:00-10:00", "10:00-11:00",
"11:00-12:00", "12:00-13:00", "13:00-14:00",
"14:00-15:00", "15:00-16:00", "16:00-17:00",
"17:00-18:00", "18:00-19:00", "19:00-20:00",
"20:00-21:00"
];
var confirmX = random(screenWidth/2, screenWidth/2 + 50);
var confirmY = random(screenHeight - 150, screenHeight - 80);
// 缓存已找到的元素
var elementCache = {};
timeSlots.forEach(function(slot) {
// 检查缓存中是否已有该元素
if (!elementCache[slot]) {
// 未缓存则查找并存入缓存
elementCache[slot] = text(slot).findOne(200);
}
var element = elementCache[slot];
if (element) {
// 点击时间段元素
element.click();
sleep(50)
click(confirmX, confirmY);
sleep(50)
}
});
无法正常执行点击
回答
你提供的脚本逻辑中,试图通过缓存机制查找时间段元素并点击,但 text(slot).findOne(200)
可能无法准确找到对应的时间段控件。此外,在 Hamibot 的环境中,使用 random()
需要确保其返回的坐标是有效的,并且目标区域可点击。
以下是一些优化建议和修正后的代码:
// 文档地址 https://docs.hamibot.com/
var timeSlots = [
"8:00-9:00", "9:00-10:00", "10:00-11:00",
"11:00-12:00", "12:00-13:00", "13:00-14:00",
"14:00-15:00", "15:00-16:00", "16:00-17:00",
"17:00-18:00", "18:00-19:00", "19:00-20:00",
"20:00-21:00"
];
// 获取屏幕尺寸
var screenWidth = device.width;
var screenHeight = device.height;
// 确定按钮的随机坐标范围
var confirmX = random(screenWidth / 2, screenWidth / 2 + 50);
var confirmY = random(screenHeight - 150, screenHeight - 80);
// 缓存已找到的元素
var elementCache = {};
timeSlots.forEach(function(slot) {
// 检查缓存中是否已有该元素
if (!elementCache[slot]) {
// 未缓存则查找并存入缓存
elementCache[slot] = textContains(slot).findOne(200); // 使用 textContains 更灵活
}
var element = elementCache[slot];
if (element) {
// 点击时间段元素
element.click();
sleep(50);
// 点击确认位置
click(confirmX, confirmY);
sleep(50);
} else {
log("未找到时间段:" + slot);
}
});
说明:
textContains(slot)
:比text(slot)
更灵活,可以匹配包含该文本的控件。- 随机坐标验证:如果
confirmX
和confirmY
是固定不变的,应该在循环外部定义;如果需要每次点击不同的随机位置,请在循环内部重新生成随机值。 - 添加日志输出:用于调试哪些时间段没有被正确识别。
请根据实际 UI 调整选择器或坐标,并测试是否满足需求。