auto.waitFor();
if (!requestScreenCapture()) {
toastLog('请授予屏幕截图权限!');
hamibot.exit();
}

console.show();
console.setSize(device.width / 2, device.height / 4);
console.setPosition(0, 100);
ui.run(function() {
let mConsoleViewField = runtime.console.getClass().getDeclaredField('mConsoleView');
mConsoleViewField.setAccessible(true);
let refConsoleView = mConsoleViewField.get(runtime.console);
let mConsoleView = refConsoleView.get();
mConsoleView.findViewById(com.stardust.autojs.R.id.input_container).visibility = android.view.View.GONE;
});

function refreshUi() {
if (typeof auto !== 'undefined' && auto.root) {
auto.root.refresh();
}
}

function completePayment() {
// 在支付开始时启动异步线程播放声音
// threads.start(function() {
// playSystemSound();
// });
const STATE_INIT = 0;
const STATE_zhejia_CLICKED = 1;
const STATE_wozhidaole_CLICKED = 2;
const STATE_queding_CLICKED = 3 ;
const STATE_COMPLETED = 4;
let currentState = STATE_INIT;
let lastActionTime = Date.now();
const TIMEOUT = 60000; // 总超时60秒
console.log("🚀 启动支付流程,超时设置:", TIMEOUT/1000 + "秒");
while (Date.now() - lastActionTime < TIMEOUT) {
auto.root.refresh();
console.log("⏳ 剩余时间:",
Math.round((TIMEOUT - (Date.now() - lastActionTime))/1000) + "秒");
switch(currentState) {
case STATE_INIT:
sleep(200);
const payBtn = className("android.widget.TextView").text("确认信息并支付").findOne(3000);
if (payBtn) {
console.log("🖱️ 找到确认信息并支付");
payBtn.click();
currentState = STATE_zhejia_CLICKED;
// lastActionTime = Date.now();
}
else{
console.log("🖱️ 没有找到确认信息并支付");
}
break;
case STATE_zhejia_CLICKED:
sleep(200);
const confirmBtn = className("android.widget.TextView").textMatches(/确认无误|就是这家/).findOne(3000);
if (confirmBtn) {
console.log("🖱️ 找到确认无误|就是这家");
confirmBtn.click();
currentState = STATE_wozhidaole_CLICKED;
// lastActionTime = Date.now();
}
else{
console.log("🖱️ 没有找到确认无误|就是这家");
}
break;
case STATE_wozhidaole_CLICKED:
sleep(200);
const confirmBtn1 = className("android.widget.TextView").textContains("我知道了").findOne(3000);
if (confirmBtn1) {
console.log("🖱️ 找到我知道了");
confirmBtn1.click();
currentState = STATE_queding_CLICKED;
// lastActionTime = Date.now();
}
else{
console.log("🖱️ 没有找到我知道了");
}
break;

        case STATE_queding_CLICKED:
            sleep(400);
            const confirmBtn2 = className("android.widget.TextView").textContains("确定").findOne(3000);
            if (confirmBtn2) {
                console.log("🖱️ 找到确定");
                confirmBtn2.click();
                currentState = STATE_COMPLETED;
                // lastActionTime = Date.now();
            }
            else{
                console.log("🖱️ 没有找到确定");    
            }
            break;
        case STATE_COMPLETED:
            console.log("🎉 支付完成");
            return true;
    }
    

    sleep(100);
}
return true;

}

completePayment();
completePayment();
completePayment();
这个函数我计划执行3次,执行第一遍时正常,第2遍时就不会动了,能够找到text,但不会执行点击操作。为什么

已存档
赛博斗战佛
1
· 2 个月前

回答

Hamibot AI
1
· 2 个月前

在你的代码中,函数执行第二遍时不点击的问题可能有以下几个原因:

  1. 状态变量未重置:每次调用completePayment()时,currentState变量没有被重置为初始状态STATE_INIT,导致第二次运行时保留了第一次的状态。

  2. 屏幕未刷新:虽然你调用了refreshUi(),但在支付流程中界面可能有多个层级,简单刷新可能不够。

  3. 点击操作可能被执行但未生效:某些支付界面可能有防频繁点击机制。

改进建议:

// 文档地址 https://docs.hamibot.com/
function completePayment() {
    const STATE_INIT = 0;
    const STATE_zhejia_CLICKED = 1;
    const STATE_wozhidaole_CLICKED = 2;
    const STATE_queding_CLICKED = 3; 
    const STATE_COMPLETED = 4;   
    let currentState = STATE_INIT; // 每次调用都会重新初始化
    
    let lastActionTime = Date.now();
    const TIMEOUT = 60000;
    console.log("🚀 启动支付流程,超时设置:", TIMEOUT/1000 + "秒");
    
    while (Date.now() - lastActionTime < TIMEOUT && currentState !== STATE_COMPLETED) {
        auto.root.refresh();    
        console.log("⏳ 剩余时间:", 
            Math.round((TIMEOUT - (Date.now() - lastActionTime))/1000) + "秒");
            
        switch(currentState) {
            case STATE_INIT:
                const payBtn = text("确认信息并支付").findOne(2000);
                if (payBtn) {
                    console.log("🖱️ 找到确认信息并支付");
                    payBtn.click();
                    currentState = STATE_zhejia_CLICKED;
                    lastActionTime = Date.now();
                    sleep(200);
                }
                break;
                
            case STATE_zhejia_CLICKED:
                const confirmBtn = textMatches(/确认无误|就是这家/).findOne(2000);
                if (confirmBtn) {
                    console.log("🖱️ 找到确认无误|就是这家");
                    confirmBtn.click();
                    currentState = STATE_wozhidaole_CLICKED;
                    lastActionTime = Date.now();
                    sleep(200);
                }
                break;
                
            case STATE_wozhidaole_CLICKED:
                const confirmBtn1 = textContains("我知道了").findOne(2000);
                if (confirmBtn1) {
                    console.log("🖱️ 找到我知道了");
                    confirmBtn1.click();
                    currentState = STATE_queding_CLICKED;
                    lastActionTime = Date.now();
                    sleep(200);
                }
                break;
            
            case STATE_queding_CLICKED:
                const confirmBtn2 = textContains("确定").findOne(2000);
                if (confirmBtn2) {
                    console.log("🖱️ 找到确定");
                    confirmBtn2.click();
                    currentState = STATE_COMPLETED;
                    lastActionTime = Date.now();
                    sleep(400);
                }
                break;
        }
        
        if(currentState !== STATE_COMPLETED) {
            sleep(200);
        }
    }
    
    console.log(currentState === STATE_COMPLETED ? "🎉 支付完成" : "❌ 支付超时");
    return currentState === STATE_COMPLETED;
}

// 测试调用
for(var i = 1; i <= 3; i++) {
    console.log("\n===== 第 " + i + " 次执行 =====");
    completePayment();
}
hamibot.exit();

主要改进点:

  1. 每次调用completePayment()都会重新初始化状态变量
  2. 增加了支付超时和完成状态的判断
  3. 优化了等待逻辑
  4. 使用循环执行3次测试
  5. 移除了多余的sleep

如果问题仍然存在,可能需要检查:

  • 支付界面的动态变化
  • 点击后是否有报错提示
  • 设备性能是否足够流畅运行脚本
ℹ️ 本回答由 AI 生成,内容仅供参考