zoukankan      html  css  js  c++  java
  • 自定义Robotframework,Appium的一个关键字(用于点击目标图片,用于Appium无法识别的一些图片元素)

     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("识别不到要点击目标图片")

    经过实际测试,识别误差太大了,有待优化。

    参考: https://www.cnblogs.com/meitian/p/7417582.html

  • 相关阅读:
    Vue|提示信息统一处理
    SpringBoot|封装接口实现自动创建job并且自动构建功能
    SpringBoot|自动创建job并且参数化构建
    SpringBoot|持久化常用注解
    SpringBoot|使用mybatis-generator-maven-plugin自动生成代码
    SpringBoot|config.properties通用Mapper配置
    SpringBoot|config.properties通用数据库连接配置
    SpringBoot|数据持久化技术现状
    Debian安装完成后没有无线网络的解决方法
    Debian系统设置terminal快捷键
  • 原文地址:https://www.cnblogs.com/brianlai/p/11607986.html
Copyright © 2011-2022 走看看