第四个模块,uiautomator2点击方法封装

1.uiautomator2的模拟点击方法classname,resourceId,test,xpath,坐标
2.我们把这些元素给封装成一个class

import time

import uiautomator2 as u2
import os
import subprocess
import logging

class UiClick(object):
    def __init__(self):
        logging.basicConfig(level=logging.DEBUG, format='%(asctime)s-%(levelname)s-%(message)s')
        """
        out是执行成功返回0
        info是执行后控制面板输出
        :return:
        """
        out, info = subprocess.getstatusoutput("adb shell pm list packags uiautomator")
        if out == 0:
            if "Error" not in info:
                logging.info("初始化OK")
            else:
                os.system("python -m uiautomator2 init")
        self.d = u2.connect()

    def ElementMethod(self, element_name):
        Method = self.Element.get(element_name)
        if Method is not None:
            if "resourceId" in Method:
                if self.d(resourceId=Method.get("resourceId")).exists:
                    self.d(resourceId=Method.get("resourceId")).click()
                    logging.info("使用", Method.get("resourceId"), "点击成功")
                    return Method.get("resourceId")
            elif "text" in Method:
                if self.d(text=Method.get("text")).exists:
                    self.d(text=Method.get("text")).click()
                    logging.info("使用", Method.get("text"), "点击成功")
                    return Method.get("text")
            elif "xpath" in Method:
                if self.d.xpath(Method.get("xpath")).exists:
                    self.d.xpath(Method.get("xpath")).click()
                    logging.info("使用", Method.get("xpath"), "点击成功")
                    return Method.get("xpath")
            elif "coord" in Method:
                # if self.d(xpath=Method.get("coord")).exists:
                self.d.click(Method.get("coord")[0], Method.get("coord")[1])
                logging.info("使用", Method.get("coord"), "点击成功")
                return Method.get("coord")[0], Method.get("coord")[1]
            else:
                logging.info("未找到元素,请检查")
                return None
        else:
            print("未找名字", element_name)
            return element_name

3.这样我们读取通过传入element_name,代码再通过我们读取到元素.json表格找到对应的element_name的元素,然后完成点击操作

Logo

欢迎加入 MCP 技术社区!与志同道合者携手前行,一同解锁 MCP 技术的无限可能!

更多推荐