基於uiautomator2 安裝 app,處理adb install 未知來源 風險管控的一個方式

有些手機可以通過adb直接安裝 app,但有些手機會提示風險提示,有的還要求輸入密碼等(oppo、vivo)

正常基於uiautomator2 安裝一個app

import uiautomator2 as u2
import os

d = u2.connect()
apk_path = os.path.normpath("./xxxx.apk")
d.app_install(apk_path)

有風險提示的一個方案

現在是通過啓動另一個線程,通過 uiautomator2 ,獲取UI遠程,然後輸入點,只能點擊位置,直接代碼了。

import adbutils
import uiautomator2 as u2
import os
import threading


def get_devices():
    # 獲取設備
    return adbutils.adb.device_list()


def install_app(d):
    apk_path = os.path.normpath("./xxxx.apk")
    d.app_install(apk_path)


def click_install(d):
    if d(text="忘記密碼").exists(timeout=30):
        d.send_keys("aA111111")
        d(text="安裝").click()

    if d(text="繼續安裝").exists(timeout=10):
        d(text="繼續安裝").click()
        d(text="安裝").click()

    if d(text="應用權限").exists():
        info = d.device_info
        x = info["display"]["width"] / 2
        y = info["display"]["height"]
        d.click(536, 2216)


if __name__ == "__main__":
    devices = get_devices()
    print(devices)
    if devices is None or devices.__len__ == 0:
        print("not found devices")

    threads = []

    for device in devices:
        d = u2.connect(device.serial)

        th1 = threading.Thread(target=install_app, args=(d,))
        th2 = threading.Thread(target=click_install, args=(d,))

        th1.start()
        th2.start()

        th1.join()
        th2.join()


PS: 但發現,有些系統是沒辦法找到繼續安裝的按鈕的,查看ui hierarchy,是沒有那個按鈕的信息,回頭再嘗試一下其它方法

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章