前期准备
设备准备
环境准备
手机环境
电脑环境
环境初始化
python -m uiautomator2 init
代码展示
import time
import uiautomator2 as u2
def main():
device: u2.Device = u2.connect()
device.app_start('com.lxkj.guagua')
time.sleep(5)
d(text='小视频').click()
with d.watch_context() as ctx:
ctx.when('//*[@resource-id="com.lxkj.guagua:id/cancel_action_f"]').click()
ctx.when('//*[@resource-id="com.lxkj.guagua:id/cancel_action"]').click()
while True:
d.swipe_ext('up', 0.5, duration=0.15)
time.sleep(5)
if d(resourceId='com.lxkj.guagua:id/egg_view').exists:
d(text='金蛋大奖').click()
break
with d.watch_context() as ctx:
ctx.when('//*[@resource-id="com.lxkj.guagua:id/iv_cancel"]').click()
while True:
if d.xpath('//*[@resource-id="android:id/content"]/android.widget.RelativeLayout[1]/android.widget.RelativeLayout[1]/android.widget.ImageView[1]').exists:
d.xpath('//*[@resource-id="android:id/content"]/android.widget.RelativeLayout[1]/android.widget.RelativeLayout[1]/android.widget.ImageView[1]').click()
break
elif d(resourceId='com.lxkj.guagua:id/tt_video_ad_close_layout').exists:
d(resourceId='com.lxkj.guagua:id/tt_video_ad_close_layout').click()
break
else:
time.sleep(1)
device.app_stop('com.lxkj.guagua')
if __name__ == '__main__':
main()
效果展示 x10
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/03b32d40bb13712925c91102bd2d36b6.gif#pic_center)