【使用 uiautomator2 脚本进行波点音乐app自动化操作的教程】

本教程将指导您使用 uiautomator2 库编写脚本,实现自动化操作。如果您需要在设备断开连接或出现异常时重新连接设备并继续执行任务,这里有一份详细的代码示例和使用说明。

环境准备
  1. 安装 Python

    • 确保您的系统上安装了 Python(推荐使用 Python 3.7 及以上版本)。
    • 可以从Python 官网下载并安装。
  2. 安装 uiautomator2

    bash 复制代码
    pip install uiautomator2
  3. 安装 ADB

    • 确保您的系统上安装了 ADB(Android Debug Bridge)。可以从ADB 下载页面下载。
  4. 连接设备

    • 确保您的 Android 设备通过 USB 连接到计算机,并且开发者模式和 USB 调试已启用。
脚本代码

以下是用于连接设备、启动 ATX-Agent、启动应用并处理广告的脚本代码:

python 复制代码
import uiautomator2 as u2
import time
import os

def connect_and_setup_device(retry_interval=1, max_retries=10):
    retries = 0
    while retries < max_retries:
        try:
            d = u2.connect()
            if d:
                print('设备连接成功')
                break
        except Exception as e:
            print(f'设备连接失败,重试 {retries + 1}/{max_retries} 次,错误: {e}')
        time.sleep(retry_interval)
        retries += 1
        retry_interval *= 2  # 指数退避算法
    else:
        raise RuntimeError('设备连接失败,达到最大重试次数。')

    # 打印设备信息
    print('设备信息:', d.info)

    # 启动 ATX-Agent
    try:
        d.shell('chmod 775 /data/local/tmp/atx-agent')
        d.shell('/data/local/tmp/atx-agent server -d')
        d.shell('/data/local/tmp/atx-agent server --nouia')
        print('ATX-Agent 启动成功')
    except Exception as e:
        print(f'启动 ATX-Agent 失败: {e}')
        raise

    print('连接完成~')
    return d

def main():
    d = connect_and_setup_device()
    os.system('adb shell am force-stop cn.wenyu.bodian')

    time.sleep(2)
    num = 1

    while True:
        try:
            # 检查设备连接状态
            d.shell('echo test')
                
            os.system('adb shell am start -n cn.wenyu.bodian/cn.wenyu.bodian.MainActivity')
            print("应用启动成功")
            
            if d(text='跳过广告').exists:
                d(text='跳过广告').click()
                print("点击:跳过广告")
                time.sleep(2)
            
            if d(description='去看看').exists:
                d.click(0.49, 0.324)
                print("点击:去看看")
                time.sleep(2)

            try:
                d.click(0.781, 0.065)
                print('点击广告入口成功')
            except:
                print('点击广告入口失败')

            if d(description='开启免费模式').exists:
                d(description='开启免费模式').click()
                print('执行:d(description=开启免费模式).click()')
            else:
                d.click(0.574, 0.078)
                print('开始看广告了d.click(0.574, 0.078)')
            
            d(description='获取更多时间').click_exists()
            start_time = time.time()

            if d(description='关闭声音').exists:
                while d(description='关闭声音').exists:
                    elapsed_time = time.time() - start_time
                    time.sleep(elapsed_time)
                    print('等待', elapsed_time, '秒')
                try:
                    d(description='关闭广告').click_exists()
                    print('广告已关闭')
                except Exception as e:
                    print("关闭广告元素不存在:", e)
            else:
                try:
                    d(text='点击一下,立即免费听歌').wait(timeout=4)
                    d(text='点击一下,立即免费听歌').click()
                    print('点击一下,立即免费听歌')
                    time.sleep(2)
                    d.keyevent('4')
                    time.sleep(2)
                    d(resourceId='cn.wenyu.bodian:id/tme_ad_skip_button').click()
                    print('广告跳过')
                except Exception as e:
                    print("广告跳过元素不存在:", e)
            
            time.sleep(3)
            num += 1
            print('执行第:', num, '次看广告')
        
        except Exception as e:
            print(f'发生异常: {e}')
            print("尝试重新连接设备...")
            d = connect_and_setup_device()

if __name__ == "__main__":
    main()
使用说明
  1. 连接和启动设备

    • connect_and_setup_device 函数用于连接设备并启动 ATX-Agent。如果连接失败,它会重试多次,并使用指数退避算法增加重试间隔。
  2. 启动应用并处理广告

    • main 函数是脚本的入口。它首先连接设备并启动应用,然后在主循环中执行自动化操作,包括点击广告入口、处理广告等。
    • 如果在执行过程中发生异常,脚本会尝试重新连接设备并继续执行。
注意事项
  1. 连接设备

    • 确保设备通过 USB 连接到计算机,并启用开发者模式和 USB 调试。
  2. 设备权限

    • 运行脚本之前,确保已授予设备必要的权限,例如存储权限和安装应用权限。
  3. 异常处理

    • 脚本包含基本的异常处理逻辑。如果遇到更复杂的错误场景,可以根据需要扩展异常处理部分。
  4. 调试和日志

    • 在实际使用过程中,可以添加更多的日志信息,以便更好地调试和监控脚本运行状态。

通过本教程,您可以使用 uiautomator2 库编写一个自动化脚本,实现设备连接、应用启动和广告处理等操作。如果遇到设备连接问题或异常,脚本会自动重新连接设备并继续执行,从而提高了自动化任务的可靠性和稳定性。

相关推荐
Xxtaoaooo20 分钟前
Nginx 502 网关错误:upstream 超时配置的踩坑与优化
运维·nginx·负载均衡·502错误·upstream超时
zzzsde1 小时前
【Linux】初识Linux
linux·运维·服务器
fouryears_234171 小时前
云服务器使用代理稳定与github通信方法
运维·服务器·github
wanhengidc2 小时前
手机云服务是什么意思?
运维·网络·安全·游戏·智能手机
desssq2 小时前
ubuntu 18.04 泰山派编译报错
linux·运维·ubuntu
zzu123zsw2 小时前
第五章:自动化脚本开发
人工智能·自动化
Dovis(誓平步青云)3 小时前
《Linux 基础指令实战:新手入门的命令行操作核心教程(第一篇)》
linux·运维·服务器
维尔切4 小时前
Shell 脚本编程:函数
linux·运维·自动化
云泽8084 小时前
从ENIAC到Linux:计算机技术与商业模式的协同演进
linux·运维·服务器
wheeldown4 小时前
【Linux】【实战向】Linux 进程替换避坑指南:从理解 bash 阻塞等待,到亲手实现能执行 ls/cd 的 Shell
linux·运维·bash