设为首页 加入收藏

TOP

python使用uiautomator2操作雷电模拟器内App(一)
2023-07-25 21:23:28 】 浏览:61
Tags:python 使用 uiautomator2 雷电模 App

(1)首先需要在需要操作雷电模拟器的电脑上安装相关环境:

1安装vc++ 14.0;
2安装python,
3安装lxml:lxml?4.9.0?cp311?cp311?win_amd64.whl
4安装uiautomator2:pip --default-time=1000 install uiautomator2
如果安装不上可以切换清华园的仓库:https://pypi.tuna.tsinghua.edu.cn/simple

如果提示安装pillow,那就执行以下命令:pip install pillow再安装uiautomator2

python -m uiautomator2
5安装查看模拟器中APP页面元素工具weditor:pip install weditor

异常处理:

python -m uiautomator2 init后报错unknown host service,但是生成了小黄车,但打开小黄车提示我无法连接。
要用\雷电\platform-tools_r29.0.5-windows下的adb替换掉雷电目录下的adb

当执行pip install weditor -i https://pypi.tuna.tsinghua.edu.cn/simple
出错windows UnicodeDecodeError: 'gbk' codec can't decode byte 0xad in position 825: illegal multibyte se

切换下版本就可以了pip install weditor==0.6.3 -i https://pypi.tuna.tsinghua.edu.cn/simple

(2)Python代码

import json
import time
import datetime
import uiautomator2 as s2b
import calendar
import time
import os

import requests

try:
    d = s2b.connect('emulator-5554')
finally:
    os.system(r'C:\shell\adb-restart.bat')
    d = s2b.connect('emulator-5554')


getWaitExchangeRecordurl = ''
saveWaitExchangeRecordurl = ''
count = 0
d(resourceId="com.cslc.mobile.terminal:id/local_account").send_keys('001')
d(resourceId="com.cslc.mobile.terminal:id/password_input").send_keys('888888')
d(resourceId="com.cslc.mobile.terminal:id/login_button").click()
time.sleep(2)
d(resourceId="com.cslc.mobile.terminal:id/img").click()
time.sleep(2)
d(resourceId="com.cslc.mobile.terminal:id/tv_manual_entry").click()
time.sleep(2)
while True:
    time.sleep(1)
    count = count + 1
    if count == 50:
        try:
            print("reset count " + time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())))
            lotteryNumbert = "360791001894920210"
            redeemNumbert = "11061557532"
            print(lotteryNumbert);
            d(resourceId="com.cslc.mobile.terminal:id/et_ticket_no_1").send_keys(lotteryNumbert[0:3])
            d(resourceId="com.cslc.mobile.terminal:id/et_ticket_no_2").send_keys(lotteryNumbert[2:6])
            d(resourceId="com.cslc.mobile.terminal:id/et_ticket_no_3").send_keys(lotteryNumbert[6:])
            d(resourceId="com.cslc.mobile.terminal:id/et_lottery_no").send_keys(redeemNumbert)
            d(resourceId="com.cslc.mobile.terminal:id/bt_lottery").click()
            count = 0
            d(resourceId="com.cslc.mobile.terminal:id/dialog_button1").click_exists(timeout=10)
        except  Exception as e:
        #     ts = calendar.timegm(time.gmtime())
            ts = time.strftime("%Y-%m-%d-%H-%M-%S", time.localtime())
            d.screenshot(ts+"exception.png")
            os.system(r'C:\shell\restart.bat')

    try:
        response = requests.get(getWaitExchangeRecordurl)
        if response.status_code != 200:
            print("please check server service")
        response = json.loads(response.content);
        if len(response["data"]) > 0:
            firstelement = response["data"][0];
            print(firstelement['lotteryNumber'][0:3]);
            # d(resourceId="com.cslc.mobile.terminal:id/local_account").send_keys('001')
            # d(resourceId="com.cslc.mobile.terminal:id/password_input").send_keys('888888')
            # d(resourceId="com.cslc.mobile.terminal:id/login_button").click()
            # time
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇【KAWAKO】从mac上定时将腾讯云的.. 下一篇Python工具箱系列(二十六)

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目