python自动化运维——模拟键盘鼠标重复性操作Pyautoui

本文主要是介绍python自动化运维——模拟键盘鼠标重复性操作Pyautoui,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

一、程序样式展示

将程序与cmd.xls文件放在同一文件夹,每一步的截图也放在当前文件夹
通过图片在屏幕上面进行比对,找到点击处进行自动化操作

自动化rpa测试

指令

二、核心点

1.Pyautoui模块:主要针对图片进行定位pyautogui.locateCenterOnScreen(),在屏幕上面找到该图片位置后进行pyautogui.click单击,双击,右键,输入操作,还有滑轮操作pyautogui.scroll,组合按键按键操作pyautogui.press(‘enter’),pyautogui.hotkey()这里使用滑轮需要先点击到滑轮处,然后进行滑动才行,不然可能会失效。


def mouseClick(clickTimes,lOrR,img,reTry):if reTry == 1:while True:location=pyautogui.locateCenterOnScreen(img,confidence=0.9)if location is not None:pyautogui.click(location.x,location.y,clicks=clickTimes,interval=0.2,duration=0.2,button=lOrR)breakprint("未找到匹配图片,0.1秒后重试")time.sleep(0.1)elif reTry == -1:while True:location=pyautogui.locateCenterOnScreen(img,confidence=0.9)if location is not None:pyautogui.click(location.x,location.y,clicks=clickTimes,interval=0.2,duration=0.2,button=lOrR)time.sleep(0.1)elif reTry > 1:i = 1while i < reTry + 1:location=pyautogui.locateCenterOnScreen(img,confidence=0.9)if location is not None:pyautogui.click(location.x,location.y,clicks=clickTimes,interval=0.2,duration=0.2,button=lOrR)print("重复")i += 1time.sleep(0.1)
def mainWork(img):i = 1while i < sheet1.nrows:#取本行指令的操作类型cmdType = sheet1.row(i)[0]if cmdType.value == 1.0:#取图片名称img = sheet1.row(i)[1].valuereTry = 1if sheet1.row(i)[2].ctype == 2 and sheet1.row(i)[2].value != 0:reTry = sheet1.row(i)[2].valuemouseClick(1,"left",img,reTry)print("单击左键",img)#2代表双击左键elif cmdType.value == 2.0:#取图片名称img = sheet1.row(i)[1].value#取重试次数reTry = 1if sheet1.row(i)[2].ctype == 2 and sheet1.row(i)[2].value != 0:reTry = sheet1.row(i)[2].valuemouseClick(2,"left",img,reTry)print("双击左键",img)#3代表右键elif cmdType.value == 3.0:#取图片名称img = sheet1.row(i)[1].value#取重试次数reTry = 1if sheet1.row(i)[2].ctype == 2 and sheet1.row(i)[2].value != 0:reTry = sheet1.row(i)[2].valuemouseClick(1,"right",img,reTry)print("右键",img) #4代表输入elif cmdType.value == 4.0:inputValue = sheet1.row(i)[1].valuepyperclip.copy(inputValue)pyautogui.hotkey('ctrl','v')time.sleep(0.5)print("输入:",inputValue)                                        #5代表等待elif cmdType.value == 5.0:#取图片名称waitTime = sheet1.row(i)[1].valuetime.sleep(waitTime)print("等待",waitTime,"秒")#6代表滚轮elif cmdType.value == 6.0:#取图片名称scroll = sheet1.row(i)[1].valuepyautogui.scroll(int(scroll))# pywinauto.mouse.scroll((700,800),-1000)print("滚轮滑动",int(scroll),"距离")  elif cmdType.value == 7.0:key = sheet1.row(i)[1].value     pyautogui.press(key)time.sleep(0.5)         print('按下',key)  elif cmdType.value == 8.0:comkey = sheet1.row(i)[1].value comkey = comkey.split('+')pyautogui.hotkey(comkey[0],comkey[1])print('按下',comkey[0] ,'+',comkey[1] )i += 1

2.读取excel文件进行数据验证,针对字符类型以及数字范围进行限制

def dataCheck(sheet1):checkCmd = True#行数检查if sheet1.nrows<2:print("没有数据")checkCmd = False#每行数据检查i = 1while i < sheet1.nrows:# 第1列 操作类型检查cmdType = sheet1.row(i)[0]if cmdType.ctype != 2 or (cmdType.value != 1.0 and cmdType.value != 2.0 and cmdType.value != 3.0 and cmdType.value != 4.0 and cmdType.value != 5.0 and cmdType.value != 6.0 and cmdType.value != 7.0 and cmdType.value != 8.0):print('第',i+1,"行,第1列数据有毛病")checkCmd = False# 第2列 内容检查cmdValue = sheet1.row(i)[1]# 读图点击类型指令,内容必须为字符串类型if cmdType.value ==1.0 or cmdType.value == 2.0 or cmdType.value == 3.0:if cmdValue.ctype != 1:print('第',i+1,"行,第2列数据有毛病")checkCmd = False# 输入类型,内容不能为空if cmdType.value == 4.0:if cmdValue.ctype == 0:print('第',i+1,"行,第2列数据有毛病")checkCmd = False# 等待类型,内容必须为数字if cmdType.value == 5.0:if cmdValue.ctype != 2:print('第',i+1,"行,第2列数据有毛病")checkCmd = False# 滚轮事件,内容必须为数字if cmdType.value == 6.0:if cmdValue.ctype != 2:print('第',i+1,"行,第2列数据有毛病")checkCmd = False#单独按键enterif cmdType.value == 7.0:if cmdValue.ctype != 1:print('第',i+1,"行,第2列数据有毛病")checkCmd = False#组合键 ctrl+a ctrl+c  ctrl+v.... if cmdType.value == 8.0:if cmdValue.ctype == 0:print('第',i+1,"行,第2列数据有毛病")checkCmd = Falsei += 1return checkCmd

三、完整代码

import pyautogui
import time
import xlrd
import pyperclip
import pywinauto.mouse#定义鼠标事件#pyautogui库其他用法 https://blog.csdn.net/qingfengxd1/article/details/108270159def mouseClick(clickTimes,lOrR,img,reTry):if reTry == 1:while True:location=pyautogui.locateCenterOnScreen(img,confidence=0.9)if location is not None:pyautogui.click(location.x,location.y,clicks=clickTimes,interval=0.2,duration=0.2,button=lOrR)breakprint("未找到匹配图片,0.1秒后重试")time.sleep(0.1)elif reTry == -1:while True:location=pyautogui.locateCenterOnScreen(img,confidence=0.9)if location is not None:pyautogui.click(location.x,location.y,clicks=clickTimes,interval=0.2,duration=0.2,button=lOrR)time.sleep(0.1)elif reTry > 1:i = 1while i < reTry + 1:location=pyautogui.locateCenterOnScreen(img,confidence=0.9)if location is not None:pyautogui.click(location.x,location.y,clicks=clickTimes,interval=0.2,duration=0.2,button=lOrR)print("重复")i += 1time.sleep(0.1)# 数据检查
# cmdType.value  1.0 左键单击    2.0 左键双击  3.0 右键单击  4.0 输入  5.0 等待  6.0 滚轮
# ctype     空:0
#           字符串:1
#           数字:2
#           日期:3
#           布尔:4
#           error:5
def dataCheck(sheet1):checkCmd = True#行数检查if sheet1.nrows<2:print("没有数据")checkCmd = False#每行数据检查i = 1while i < sheet1.nrows:# 第1列 操作类型检查cmdType = sheet1.row(i)[0]if cmdType.ctype != 2 or (cmdType.value != 1.0 and cmdType.value != 2.0 and cmdType.value != 3.0 and cmdType.value != 4.0 and cmdType.value != 5.0 and cmdType.value != 6.0 and cmdType.value != 7.0 and cmdType.value != 8.0):print('第',i+1,"行,第1列数据有毛病")checkCmd = False# 第2列 内容检查cmdValue = sheet1.row(i)[1]# 读图点击类型指令,内容必须为字符串类型if cmdType.value ==1.0 or cmdType.value == 2.0 or cmdType.value == 3.0:if cmdValue.ctype != 1:print('第',i+1,"行,第2列数据有毛病")checkCmd = False# 输入类型,内容不能为空if cmdType.value == 4.0:if cmdValue.ctype == 0:print('第',i+1,"行,第2列数据有毛病")checkCmd = False# 等待类型,内容必须为数字if cmdType.value == 5.0:if cmdValue.ctype != 2:print('第',i+1,"行,第2列数据有毛病")checkCmd = False# 滚轮事件,内容必须为数字if cmdType.value == 6.0:if cmdValue.ctype != 2:print('第',i+1,"行,第2列数据有毛病")checkCmd = False#单独按键enterif cmdType.value == 7.0:if cmdValue.ctype != 1:print('第',i+1,"行,第2列数据有毛病")checkCmd = False#组合键 ctrl+a ctrl+c  ctrl+v.... if cmdType.value == 8.0:if cmdValue.ctype == 0:print('第',i+1,"行,第2列数据有毛病")checkCmd = Falsei += 1return checkCmd#任务
def mainWork(img):i = 1while i < sheet1.nrows:#取本行指令的操作类型cmdType = sheet1.row(i)[0]if cmdType.value == 1.0:#取图片名称img = sheet1.row(i)[1].valuereTry = 1if sheet1.row(i)[2].ctype == 2 and sheet1.row(i)[2].value != 0:reTry = sheet1.row(i)[2].valuemouseClick(1,"left",img,reTry)print("单击左键",img)#2代表双击左键elif cmdType.value == 2.0:#取图片名称img = sheet1.row(i)[1].value#取重试次数reTry = 1if sheet1.row(i)[2].ctype == 2 and sheet1.row(i)[2].value != 0:reTry = sheet1.row(i)[2].valuemouseClick(2,"left",img,reTry)print("双击左键",img)#3代表右键elif cmdType.value == 3.0:#取图片名称img = sheet1.row(i)[1].value#取重试次数reTry = 1if sheet1.row(i)[2].ctype == 2 and sheet1.row(i)[2].value != 0:reTry = sheet1.row(i)[2].valuemouseClick(1,"right",img,reTry)print("右键",img) #4代表输入elif cmdType.value == 4.0:inputValue = sheet1.row(i)[1].valuepyperclip.copy(inputValue)pyautogui.hotkey('ctrl','v')time.sleep(0.5)print("输入:",inputValue)                                        #5代表等待elif cmdType.value == 5.0:#取图片名称waitTime = sheet1.row(i)[1].valuetime.sleep(waitTime)print("等待",waitTime,"秒")#6代表滚轮elif cmdType.value == 6.0:#取图片名称scroll = sheet1.row(i)[1].valuepyautogui.scroll(int(scroll))# pywinauto.mouse.scroll((700,800),-1000)print("滚轮滑动",int(scroll),"距离")  elif cmdType.value == 7.0:key = sheet1.row(i)[1].value     pyautogui.press(key)time.sleep(0.5)         print('按下',key)  elif cmdType.value == 8.0:comkey = sheet1.row(i)[1].value comkey = comkey.split('+')pyautogui.hotkey(comkey[0],comkey[1])print('按下',comkey[0] ,'+',comkey[1] )i += 1if __name__ == '__main__':file = 'cmd.xls'#打开文件wb = xlrd.open_workbook(filename=file)#通过索引获取表格sheet页# sheet1 = wb.sheet_by_index(0)print('欢迎使用Henry自动化工具')sheetname = input('请输入需要执行的工作表表名: ')sheet1 = wb.sheet_by_name(sheetname) #数据检查   checkCmd = dataCheck(sheet1)if checkCmd: key=input('选择功能: 1.执行一次 2.循环到死 \n')if key=='1':#循环拿出每一行指令mainWork(sheet1)elif key=='2':while True:mainWork(sheet1)time.sleep(0.2)print("等待0.2秒")    else:print('输入有误或者已经退出!')

这篇关于python自动化运维——模拟键盘鼠标重复性操作Pyautoui的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/354422

相关文章

Python按照24个实用大方向精选的上千种工具库汇总整理

《Python按照24个实用大方向精选的上千种工具库汇总整理》本文整理了Python生态中近千个库,涵盖数据处理、图像处理、网络开发、Web框架、人工智能、科学计算、GUI工具、测试框架、环境管理等多... 目录1、数据处理文本处理特殊文本处理html/XML 解析文件处理配置文件处理文档相关日志管理日期和

Python标准库datetime模块日期和时间数据类型解读

《Python标准库datetime模块日期和时间数据类型解读》文章介绍Python中datetime模块的date、time、datetime类,用于处理日期、时间及日期时间结合体,通过属性获取时间... 目录Datetime常用类日期date类型使用时间 time 类型使用日期和时间的结合体–日期时间(

使用Python开发一个Ditto剪贴板数据导出工具

《使用Python开发一个Ditto剪贴板数据导出工具》在日常工作中,我们经常需要处理大量的剪贴板数据,下面将介绍如何使用Python的wxPython库开发一个图形化工具,实现从Ditto数据库中读... 目录前言运行结果项目需求分析技术选型核心功能实现1. Ditto数据库结构分析2. 数据库自动定位3

Python yield与yield from的简单使用方式

《Pythonyield与yieldfrom的简单使用方式》生成器通过yield定义,可在处理I/O时暂停执行并返回部分结果,待其他任务完成后继续,yieldfrom用于将一个生成器的值传递给另一... 目录python yield与yield from的使用代码结构总结Python yield与yield

python使用Akshare与Streamlit实现股票估值分析教程(图文代码)

《python使用Akshare与Streamlit实现股票估值分析教程(图文代码)》入职测试中的一道题,要求:从Akshare下载某一个股票近十年的财务报表包括,资产负债表,利润表,现金流量表,保存... 目录一、前言二、核心知识点梳理1、Akshare数据获取2、Pandas数据处理3、Matplotl

Django开发时如何避免频繁发送短信验证码(python图文代码)

《Django开发时如何避免频繁发送短信验证码(python图文代码)》Django开发时,为防止频繁发送验证码,后端需用Redis限制请求频率,结合管道技术提升效率,通过生产者消费者模式解耦业务逻辑... 目录避免频繁发送 验证码1. www.chinasem.cn避免频繁发送 验证码逻辑分析2. 避免频繁

精选20个好玩又实用的的Python实战项目(有图文代码)

《精选20个好玩又实用的的Python实战项目(有图文代码)》文章介绍了20个实用Python项目,涵盖游戏开发、工具应用、图像处理、机器学习等,使用Tkinter、PIL、OpenCV、Kivy等库... 目录① 猜字游戏② 闹钟③ 骰子模拟器④ 二维码⑤ 语言检测⑥ 加密和解密⑦ URL缩短⑧ 音乐播放

python panda库从基础到高级操作分析

《pythonpanda库从基础到高级操作分析》本文介绍了Pandas库的核心功能,包括处理结构化数据的Series和DataFrame数据结构,数据读取、清洗、分组聚合、合并、时间序列分析及大数据... 目录1. Pandas 概述2. 基本操作:数据读取与查看3. 索引操作:精准定位数据4. Group

Python pandas库自学超详细教程

《Pythonpandas库自学超详细教程》文章介绍了Pandas库的基本功能、安装方法及核心操作,涵盖数据导入(CSV/Excel等)、数据结构(Series、DataFrame)、数据清洗、转换... 目录一、什么是Pandas库(1)、Pandas 应用(2)、Pandas 功能(3)、数据结构二、安

Python使用Tenacity一行代码实现自动重试详解

《Python使用Tenacity一行代码实现自动重试详解》tenacity是一个专为Python设计的通用重试库,它的核心理念就是用简单、清晰的方式,为任何可能失败的操作添加重试能力,下面我们就来看... 目录一切始于一个简单的 API 调用Tenacity 入门:一行代码实现优雅重试精细控制:让重试按我