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.sleep(3)
            # d.click(0.189, 0.221)
            # time.sleep(3)
            # print(d(resourceId="com.cslc.mobile.terminal:id/dialog_text").get_text())
            # d(resourceId="com.cslc.mobile.terminal:id/tv_manual_entry").click()
            d(resourceId="com.cslc.mobile.terminal:id/et_ticket_no_1").send_keys(firstelement['lotteryNumber'][0:3])
            d(resourceId="com.cslc.mobile.terminal:id/et_ticket_no_2").send_keys(firstelement['lotteryNumber'][2:6])
            d(resourceId="com.cslc.mobile.terminal:id/et_ticket_no_3").send_keys(firstelement['lotteryNumber'][6:])
            d(resourceId="com.cslc.mobile.terminal:id/et_lottery_no").send_keys(firstelement['redeemNumber'])
            d(resourceId="com.cslc.mobile.terminal:id/bt_lottery").click()
            count = 0
            if (d(resourceId="com.cslc.mobile.terminal:id/dialog_text").exists(timeout=2)
               and not (d(resourceId='com.cslc.mobile.terminal:id/tv_bonus').exists(timeout=2))):
                print(d(resourceId="com.cslc.mobile.terminal:id/dialog_text").get_text());
                redeemResult = d(resourceId="com.cslc.mobile.terminal:id/dialog_text").get_text();
                lotteryNumber = firstelement['lotteryNumber']
                redeemNumber = firstelement['redeemNumber']
                print(lotteryNumber)
                print(redeemNumber)
                dataPost = {
                    "lotteryNumber": lotteryNumber,
                    "redeemNumber": redeemNumber,
                    "redeemResult": redeemResult
                }
                req = requests.post(saveWaitExchangeRecordurl, json=dataPost)
                print(req);
                d(resourceId="com.cslc.mobile.terminal:id/dialog_button1").click()
            elif d(resourceId="com.cslc.mobile.terminal:id/tv_bonus").exists(timeout=2):
                    print(d(resourceId="com.cslc.mobile.terminal:id/tv_bonus").get_text());
                    #redeemResult = d(resourceId="com.cslc.mobile.terminal:id/tv_total_bonus_of_lottery").get_text();
                    redeemResult = d(resourceId="com.cslc.mobile.terminal:id/tv_bonus").get_text();
                    lotteryNumber = firstelement['lotteryNumber']
                    redeemNumber = firstelement['redeemNumber']
                    print(lotteryNumber)
                    print(redeemNumber)
                    dataPost = {
                        "lotteryNumber": lotteryNumber,
                        "redeemNumber": redeemNumber,
                        "redeemResult": redeemResult
                    }
                    req = requests.post(saveWaitExchangeRecordurl, json=dataPost)
                    print(req);
                    # d(resourceId="com.cslc.mobile.terminal:id/title").click()
                    # time.sleep(2)
                    # d(resourceId="com.cslc.mobile.terminal:id/title").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)
            # d(resourceId="com.cslc.mobile.terminal:id/dialog_button1").click()
            # d(resourceId="com.cslc.mobile.terminal:id/title").click()
        else:
            print("no data " + time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())))
    except  Exception as e:
        print(e)
        #raise ConnectionError(f'{getWaitExchangeRecordurl} status code is {response.status_code}.')

效果如下:

posted @ 2023-02-23 14:30  tuohaibei  阅读(1793)  评论(0编辑  收藏  举报