【使用 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 库编写一个自动化脚本,实现设备连接、应用启动和广告处理等操作。如果遇到设备连接问题或异常,脚本会自动重新连接设备并继续执行,从而提高了自动化任务的可靠性和稳定性。

相关推荐
技术小齐1 小时前
网络运维学习笔记 016网工初级(HCIA-Datacom与CCNA-EI)PPP点对点协议和PPPoE以太网上的点对点协议(此处只讲华为)
运维·网络·学习
ITPUB-微风1 小时前
Service Mesh在爱奇艺的落地实践:架构、运维与扩展
运维·架构·service_mesh
落幕2 小时前
C语言-进程
linux·运维·服务器
chenbin5202 小时前
Jenkins 自动构建Job
运维·jenkins
java 凯2 小时前
Jenkins插件管理切换国内源地址
运维·jenkins
AI服务老曹2 小时前
运用先进的智能算法和优化模型,进行科学合理调度的智慧园区开源了
运维·人工智能·安全·开源·音视频
sszdzq4 小时前
Docker
运维·docker·容器
book01214 小时前
MySql数据库运维学习笔记
运维·数据库·mysql
唐古乌梁海4 小时前
【pytest】编写自动化测试用例命名规范README
自动化·pytest
bugtraq20215 小时前
XiaoMi Mi5(gemini) 刷入Ubuntu Touch 16.04——安卓手机刷入Linux
linux·运维·ubuntu