代码如下:
GetData.py
import xlrd class ReadExcel(): def __init__(self,file): self.open_excel = xlrd.open_workbook(file) # 获取Sheet名 def GetSheetName(self): sheet_name = self.open_excel.sheet_names() return sheet_name # 获取数据 def ExcelData(self,sheetname): sheet = self.open_excel.sheet_by_name(sheetname) # 第一行作为dict的key keys = sheet.row_values(0) # nrows:总行数,ncols:总列数 nrows,ncols = sheet.nrows,sheet.ncols # 整个sheet的数据 y = [] if nrows < 2: print ('总行数少于1') else: # 循环取行的数据 for i in range(1,nrows): # 一行的数据 k = {} # 循环取列的数据 for j in sheet.row_values(i): # 获取当前取的数据下标 data_index = sheet.row_values(i).index(j) # 写入字典,第一行作为key k[keys[data_index]] = j y.append(k) return y if __name__ == '__main__': E = ReadExcel(r'./Operation.xlsx') print (E.GetSheetName(),E.ExcelData('Sheet1'))
GetPosition.py
import aircv as ac import sys def matchImg(imgsrc,imgobj,confidencevalue=0.9):#imgsrc=原始图像,imgobj=待查找的图片 imsrc = ac.imread(imgsrc) imobj = ac.imread(imgobj) match_result = ac.find_all_template(imsrc,imobj,confidencevalue) # {'confidence': 0.5435812473297119, 'rectangle': ((394, 384), (394, 416), (450, 384), (450, 416)), 'result': (422.0, 400.0)} if match_result: return match_result[0]['result'] else: input('没有找到图片,结束运行') sys.exit() if __name__ == '__main__': position = matchImg(r'.\imgobj.png',r'F:\test\rrrr\222.png',confidencevalue = 0.9)
RunData.py
from GetData import ReadExcel from GetPosition import matchImg from selenium import webdriver from selenium.webdriver.common.action_chains import ActionChains import time class RunData(): def __init__(self): self.driver = webdriver.Chrome() def ProcessExcelData(self,data): for i in data: if i['操作图片路径']: self.driver.get_screenshot_as_file(r'.\imgobj.png') size = matchImg(r'.\imgobj.png',r'%s'%(i['操作图片路径'])) time.sleep(1) if i['操作方式'] == '输入网址': self.driver.get(i['值']) elif i['操作方式'] == '输入': ActionChains(self.driver).move_by_offset(size[0], size[1]).click().send_keys(i['值']).perform() ActionChains(self.driver).move_by_offset(-size[0], -size[1]).perform() elif i['操作方式'] == '点击': ActionChains(self.driver).move_by_offset(size[0], size[1]).click().perform() ActionChains(self.driver).move_by_offset(-size[0], -size[1]).perform() elif i['操作方式'] == '等待': time.sleep(eval(i['值'])) elif i['操作方式'] == '关闭浏览器': self.driver.quit() if __name__ == '__main__': E = ReadExcel(r'./Operation.xlsx') Sheet = E.GetSheetName() for j in Sheet: TestData = E.ExcelData(j) R = RunData() R.ProcessExcelData(TestData)
原理;
GetData.py获取excel操作步骤,GetPosition.py通过截图获取当前页面操作的坐标点,再通过RunData.py文件进行操作
使用步骤:
1.截取需要操作的元素
2.在excel输入信息,可以多个sheet
3.运行RunData.py文件