桌面软件/exe程序软件自动化大宝剑--lackey之二次封装以及selenium模仿
from pywinauto import application from pywinauto import clipboard from pywinauto.keyboard import SendKeys import win32api import win32con import os, sys, time import lackey import logging import pywinauto as pg # lackey 二次封装 class lackeyAtion(object): #初始化,有需要再加 def __init__(self): self.lackey = lackey.Screen(0) self.lackey.setAutoWaitTimeout(30) #截屏需要保存路径,截图的名字 def screen_shot(self,path,name): try: self.lackey.saveScreenCapture(path,name) except: logging.exception("screeshoot fail!!!") raise #按照坐标和宽高截图保存 def region_screenImage(self,path,name,x,y,w,h): try: lackey.Region(x,y,w,h).saveScreenCapture(path,name) except: logging.exception("find region start screenshot fail!") raise #等待显现 def wait_appear(self,pattern,description,path): try: res = self.lackey.wait(pattern) return res except: logging.exception("is not appear{}".format(description)) self.screen_shot(path,description) raise # 点击 依赖是否需要time等待 def click_left(self,pattern,description,path,timeFlag): """ :param pattern: 要找区域 :param description: 要找区域描述 :param path: 图片保存位置 :param timeFlag: 给等待时间 :return: """ if timeFlag is None: try: self.lackey.click(pattern) except: logging.exception("click {} fail!!!".format(description)) self.screen_shot(path,description) raise else: try: time.sleep(timeFlag) self.lackey.click(pattern) except: logging.exception("click {} fail!!!".format(description)) self.screen_shot(path,description) raise # 按照偏移量进行点击操作 def click_targetOffset(self,pattern,description,path,timeFlag,x,y): """ :param pattern: 要找区域 :param description: 要找区域描述 :param path: 图片保存位置 :param timeFlag: 给等待时间 :param positon: 坐标x,y :return: """ if timeFlag is None: try: self.lackey.click(lackey.Pattern(pattern).targetOffset(x,y)) except: logging.exception("click {} fail!!!".format(description)) self.screen_shot(path,description) raise else: try: time.sleep(timeFlag) self.lackey.click(lackey.Pattern(pattern).targetOffset(x,y)) except: logging.exception("click {} fail!!!".format(description)) self.screen_shot(path,description) raise # 双击 依赖是否需要time等待 def doubleClick(self,pattern,description,path,timeFlag): """ :param pattern: 要找区域 :param description: 要找区域描述 :param path: 图片保存位置 :param timeFlag: 给等待时间 :return: """ if timeFlag is None: try: self.lackey.doubleClick(pattern) except: logging.exception("dobuleclick {} fail!!!".format(description)) self.screen_shot(path,description) raise else: try: time.sleep(timeFlag) self.lackey.doubleClick(pattern) except: logging.exception("dobuleclick {} fail!!!".format(description)) self.screen_shot(path,description) raise # 按照偏移量进行双击操作 def doubleClick_targetOffset(self,pattern,description,path,timeFlag,x,y): """ :param pattern: 要找区域 :param description: 要找区域描述 :param path: 图片保存位置 :param timeFlag: 给等待时间 :param positon: 坐标x,y :return: """ if timeFlag is None: try: self.lackey.doubleClick(lackey.Pattern(pattern).targetOffset(x,y)) except: logging.exception("dobuleclick {} fail!!!".format(description)) self.screen_shot(path,description) raise else: try: time.sleep(timeFlag) self.lackey.doubleClick(lackey.Pattern(pattern).targetOffset(x,y)) except: logging.exception("dobuleclick {} fail!!!".format(description)) self.screen_shot(path,description) raise # 右键 def click_right(self,pattern,description,path,timeFlag): """ :param pattern: 要找区域 :param description: 要找区域描述 :param path: 图片保存位置 :param name: 参照描述 :param timeFlag: 给等待时间 :return: """ if timeFlag is None: try: self.lackey.rightClick(pattern) except: logging.exception("click {} fail!!!".format(description)) self.screen_shot(path,description) raise else: try: time.sleep(timeFlag) self.lackey.rightClick(pattern) except: logging.exception("click {} fail!!!".format(description)) self.screen_shot(path,description) raise # 拖拽 依赖是否需要time等待 def drag_appear(self,description,path,name,dragFrom,timeFlag,dragTo=None): """ :param description: 要找区域描述 :param path: 图片保存位置 :param name: 图片描述 :param dragFrom: 拖动起始位置 :param dragTo: 拽到终止位置 :param timeFlag: 给等待时间 :return: """ if timeFlag is None: try: self.lackey.dragDrop(dragFrom,dragTo) except: logging.exception("drag {} fail!!!".format(description)) self.screen_shot(path,name) raise else: try: time.sleep(timeFlag) self.lackey.dragDrop(dragFrom,dragTo) except: logging.exception("drag {} fail!!!".format(description)) self.screen_shot(path,name) raise def input(self,description,path,content): try: self.lackey.type(content) logging.info("input {}".format(content)) except: logging.exception("input {} fail!!!".format(description)) self.screen_shot(path,description) raise # 下拉菜单,菜单区域-菜单选项 def selector(self,pattern,name1,loc,description,path,steps,scroll_num): """ :param pattern: 菜单区域 :param name1: 菜单区域描述 :param loc: 菜单选项 :param description: 菜单选项描述 :param path: 图片保存位置 :param steps: 滚动步数 :param scrop_num: 滚动次数 :return: """ try: self.lackey.click(pattern) try: if self.wait_appear(pattern=loc,description=description,path=path): try: self.lackey.click(loc) except: logging.exception("click {} fail!!!".format(description)) self.screen_shot(path,description) raise else: for i in range(scroll_num): self.lackey.wheel(1, steps) if self.wait_appear(pattern=loc,description=description,path=path): try: self.lackey.click(loc) break except: logging.exception("click {} fail!!!".format(description)) self.screen_shot(path,description) raise logging.info("finding {}".format(description)) except: logging.exception("wait {}is not appear}".format(description)) self.screen_shot(path,description) raise except: logging.exception("click{} fail!!!".format(name1)) self.screen_shot(path,name1) raise #上传文件 def upload(self, pattern, file_path, path, description, x,y,positon=None): """ :param pattern: 上传区域/参照区域 :param file_path: 文件路径 :param path: 图片保存位置 :param description: 描述 :param positon: 决定是否偏移量坐标 x,y :return: """ if positon is None: try: self.lackey.click(pattern) self.lackey.type(file_path) time.sleep(1.5) self.lackey.type("{ENTER}{ENTER}{ENTER}") except: logging.exception("upload {} fail !!") self.screen_shot(path, description) raise else: try: self.lackey.click(lackey.Pattern(pattern).targetOffset(x,y)) self.lackey.type(file_path) time.sleep(1.5) self.lackey.type("{ENTER}{ENTER}{ENTER}") except: logging.exception("upload {} fail !!") self.screen_shot(path, description) raise # # if __name__ == '__main__': # lackeyAtion().selector(pattern=1,loc=3)
这里附上模仿selenium的网页自动化封装分层思想