App自动化实战(3) - 图片识别随机键盘

2017-11-02  本文已影响158人  yytester
问题: 操作时弹出普通窗口以及输入密码时的随机软键盘,由于焦点没有切换过来,导致无法直接通过id,xpath定位

解决方式:

import cv2
import numpy as np
import time
#定义找图方法,参数分别是:self, 原图,要找的图片部分,匹配度
    def get_pay_keyboard_number_location(self,impath, target,fit_num): #fit_num是匹配度,如 0.95,0.85

        print("start find pic")
        positions = {}

        start = time.time()
        img_rgb = cv2.imread(impath)

        teNum = "done"

        template = cv2.imread(target)
        h, w = template.shape[:-1]

        res = cv2.matchTemplate(img_rgb, template, cv2.TM_CCOEFF_NORMED)
        threshold = fit_num  # 匹配度参数,1为完全匹配
        loc = np.where(res >= threshold)
        if len(loc) > 0:
            positions[teNum] = zip(*loc[::-1])[0]
        else:
            print("Can not found number: ")

        end = time.time()
        print(end - start)

        return positions[teNum]

然后调这个方法:

#截屏
self.driver.get_screenshot_as_file('/Users/xxx/PycharmProjects/test/jianpan.jpg')


#原图
impath = '/Users/xxx/PycharmProjects/test/jianpan.jpg'

#要找的部分图
targetPath = '/Users/xxx/PycharmProjects/test/Done.jpg'


ls = self.get_pay_keyboard_number_location(impath, targetPath,0.85)


dd = ls[0]
kk = ls[1]
m = (dd/2, kk / 2)

#点击
self.driver.tap([m],20)
上一篇 下一篇

猜你喜欢

热点阅读