我正在尝试在 Android 上自动执行任务,但是当我在 Android 上的 Pydroid3 上运行我的代码时,它会返回一个错误...
我的设备是 Asus Zenfone Selfie 4
我不明白这个错误,因为错误是关于 ADB EXE 问题。
Android有ADB EXE吗?
代码:
#MODULES IMPORT
import os
import cv2
import numpy as np
from uiautomator2 import Device
from uiautomator2 import connect
#CLASS AND FUNCTIONS DEFINE
class AndroidAuto:
def _init_ (self, Serial = "Serial"):
self.Serial = Serial
def Android_Screenshot(self, dir = ".screenshots"):
dir = dir
if not os.path.exists(dir):
os.makedirs(dir)
device = Device(self.Serial)
device.screenshot(f"{dir}/.screenshot_opencv.png")
return (f"{dir}/.screenshot_opencv.png")
def Android_Touch(self, x = 0, y = 0):
device = Device(self.Serial)
device.click(x, y)
def Android_Get_Position(self, target = None):
Device.toast("Starting ")
while True:
template = AndroidAuto().Android_Screenshot()
w, h = target.shape[:-1]
res = cv2.matchTemplate(template, target, cv2.TM_CCOEFF_NORMED)
threshold = 0.8
loc = np.where(res >= threshold)
if len(loc[0]) > 0:
x, y = loc[::-1]
return x, y
else:
x, y = None, None
Device.toast("Trying again!")
continue
def Android_Find_Click(self, target = "", x = 0, y = 0):
while True:
match = AndroidAuto().Android_Get_Position(target)
if match != None:
AndroidAuto().Android_Touch(x, y = match)
break
else:
continue
AA = AndroidAuto()
print(AA.Android_Screenshot())
然后当我玩的时候,它返回给我这是错误:
RuntimeError: No adb exe could be found. install adb on your system
这就是网站描述所说的:
python-uiautomator2 是一个 python 包装器,它允许:
- 在计算机上使用 Python 编写脚本
- 在没有usb连接的情况下用电脑控制手机。
所以它只能通过 ADB 连接在 PC 上使用,而不能从 Android 本身使用。要连接到设备,uiautomator2 会搜索 ADB.EXE,但无法找到它,并向您显示错误。