Appium无法识别的一些图片元素,必须先通过图片找坐标,进而通过点击坐标解决问题。
1.先在terminer运行安装命令:
pip install robotframework-appiumlibrary
在目录:/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/AppiumLibrary/keywords 下的_element.py 添加以下方法(click_img)就好了。
2.代码如下:
# -*- coding: utf-8 -*- from AppiumLibrary.locators import ElementFinder from .keywordgroup import KeywordGroup from robot.libraries.BuiltIn import BuiltIn import ast from unicodedata import normalize from selenium.webdriver.remote.webelement import WebElement from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC,expected_conditions from appium.webdriver.common.touch_action import TouchAction from time import sleep import aircv as ac import cv2 as cv import numpy as np def click_img(self,path): """ 点击给定的目标截图 :param path: 目标图路径 :param confidence: 最低相似度 """ print("开始寻找目标图片") driver = self._current_application() sleep(2) driver.save_screenshot('screen.png') #截屏手机 #x, y 获得手机d的宽高尺寸 phoneScreen_width, phoneScreen_height = driver.get_window_size()['width'] , driver.get_window_size()['height'] imsrc = ac.imread("screen.png") # 截图的宽高 #print(imsrc.shape) capPicture_width , capPicture_height = imsrc.shape[1],imsrc.shape[0] imobj = ac.imread(path) # confidencevalue=0.5 imgsrc=原始图像,imgobj=待查找的图片, 获取匹配图片在原始图片上的中心坐标点 match_result = ac.find_template(imsrc,imobj,rgb=True) print(match_result) if match_result != None : x1, y1 = match_result['result'] #匹配图片在原始图片上的中心坐标点,也就是我们要找的点击点 #print(x1, y1) position_x, position_y = int(phoneScreen_width/capPicture_width * x1),int(phoneScreen_height/capPicture_height * y1) driver.tap([(position_x, position_y)]) name = path.split("/")[-1].split(".")[0] print('点击 "%s" 坐标:-->(%s,%s)' %(name,position_x, position_y)) else: print("识别不到要点击目标图片")
经过实际测试,识别误差太大了,有待优化。