更新20240606
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()
iqoo neo 8 pro设备
import uiautomator2 as u2
import time
from datetime import datetime
import os
# 连接设备并启动 ATX-Agent
d = u2.connect()
os.system('adb shell chmod 775 /data/local/tmp/atx-agent')
os.system('adb shell /data/local/tmp/atx-agent server -d')
os.system('adb shell /data/local/tmp/atx-agent server --nouia')
os.system('adb shell am force-stop cn.wenyu.bodian')
time.sleep(2)
# 打印设备信息
print(d.info)
num = 1
while True:
try:
os.system('adb shell am start -n cn.wenyu.bodian/cn.wenyu.bodian.MainActivity')
print("应用启动成功")
except Exception as e:
print("应用启动失败:", e)
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, '次看广告')
import uiautomator2 as u2
import time
from datetime import datetime
import xml.etree.ElementTree as ET
import re
import os
# 连接设备
d = u2.connect()
os.system('adb shell chmod 775 /data/local/tmp/atx-agent')
os.system('adb shell /data/local/tmp/atx-agent server -d')
# 打印设备信息
print(d.info)
def print_description_to_file(d):
# 获取当前页面的 XML结构
xml_content = d.dump_hierarchy()
# 解析 XML结构
root = ET.fromstring(xml_content)
# 定义正则表达式
time_pattern = re.compile(r'\d{2}:\d{2}:\d{2}')
with open('test.txt', 'a') as f:
# 遍历所有元素
for element in root.iter():
# 检查元素是否可点击
if element.attrib.get('clickable', 'false').lower() == 'true':
# 获取 description 属性值
description = element.attrib.get('content-desc', '')
# 使用正则表达式匹配
if time_pattern.match(description):
# 写入文件
print("Description 属性值:", description)
f.write("Description 属性值: " + description + '\n')
def swipe_left_to_right(d):
width, height = d.window_size()
start_x = width // 4
start_y = height // 2
end_x = width * 3 // 4
end_y = height // 2
d.swipe(start_x, start_y, end_x, end_y)
def screenshot_png():
now = datetime.now().strftime("%Y_%m_%d_%H%M_%S")
d.screenshot(f'{now}.png')
# d.app_uninstall("cn.wenyu.bodian")
# d.app_install("cn.wenyu.bodian.apk")
# d.app_clear("cn.wenyu.bodian")
num=1
while True:
# 启动应用
try:
d.app_start("cn.wenyu.bodian", stop=True,wait=True)
print("应用启动成功")
except Exception as e:
print("应用启动失败:", e)
exit(1)
try:
# d(text="允许").click()
# d.xpath('//*[@resource-id="android:id/content"]/android.widget.FrameLayout[1]/android.view.View[1]/android.view.View[1]/android.view.View[1]/android.widget.ImageView[2]').click()
# print("点击同意:pass")
# d(description='微信登录领VIP').click()
# print("点击微信登录领VIP:pass")
# # 点击同意并继续
# d(description='同意并继续').click()
# print("点击微同意并继续:pass")
pass
except Exception as e:
print("账号已登录元素不存在:", e)
if d(text='跳过广告').exists:
d(text='跳过广告').click()
print("点击:跳过广告")
time.sleep(2)
else:
pass
if d(description='去看看').exists:
d.click(0.49, 0.324)
print("点击:非去看看")
time.sleep(2)
else:
pass
# # 登录后点击我的
print_description_to_file(d)
time.sleep(2)
d.click(0.681, 0.074)
# d.xpath('//*[@resource-id="android:id/content"]/android.widget.FrameLayout[1]/android.view.View[1]/android.view.View[1]/android.view.View[1]/android.widget.ImageView[5]').click_exists()
# time.sleep(6)
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('看的广告1')
except Exception as e:
print("此元素不存在:", e)
else:
try:
d(text='点击一下,立即免费听歌').wait(timeout=4) # 等待 4 秒
time.sleep(3)
d(text='点击一下,立即免费听歌').click()
print('点击一下,立即免费听歌')
time.sleep(3)
d.keyevent('4')
time.sleep(2)
d(resourceId='cn.wenyu.bodian:id/tme_ad_skip_button').click()
print('看的广告2')
except Exception as e:
print("此元素不存在:", e)
print("马上执行忘了")
time.sleep(3)
num=num+1
print('执行第:',num,'次看广告')