一个简单的自动点击脚本
配置文件张这样,需要的自己截图
excel配置文件
#!/usr/bin/python3
# sys.path.append("C:\\Users\苏\\.vscode\\extensions\\ms-python.vscode-pylance-2023.10.50\\dist\\typeshed-fallback\\stubs\\PyAutoGUI")
# print(sys.path)
import sys
import pyautogui as pag
import xlrd
import time
import pyperclip as pc
import datetime
import os
import logging as log
# 配置日志记录器
log.basicConfig(filename='./log/example.log', format='%(asctime)s.%(msecs)03d %(levelname)s: %(message)s',level=log.DEBUG)
# import opencv-python
pag.PAUSE = 0.04
confPath = 'F:\桌面\qqSpeedScript\\conf\\' #配置excel的目录
excelPath = confPath + 'ttt.xls' #配置的excel文件
runTimeStamp = time.time()
#坐标,点击次数,鼠标哪个键
def mouseClick(x,y, times, button):
if times <= 1:
pag.click(x,y)
return
for idx in range(times-1):
pag.click(x,y)
#图片名 confPath+name
def getImgLoc(name):
location = (0,0,0,0)
for i in range(5):
try:
location = pag.locateOnScreen('./conf/'+name, confidence=0.6)
# location = pag.locateOnScreen('./conf/'+name)
# print('I:getImgLoc:', confPath+name, location)
except:
log.info("Unexpected error:", i, sys.exc_info()[0], confPath+name)
print("Unexpected error:", i, sys.exc_info()[0], confPath+name)
if location is not None:
return pag.center(location) # 找图的中心点
else:
print('W:notFind Img')
def runTime(pre):
global runTimeStamp
now = time.time()
print(pre + ':' + str(now - runTimeStamp))
runTimeStamp = now
def ctrlV():
pag.keyDown('ctrl')
pag.keyDown('v')
pag.keyUp('v')
pag.keyUp('ctrl')
def sleepToDesignatedTime(expectedTime):
if len(expectedTime) > 6:
expectedTime = expectedTime[0:6]
# 指定时间
specified_time = datetime.datetime(*expectedTime).timestamp()
# 当前时间
current_time = datetime.datetime.now().timestamp()
diffTime = (specified_time-current_time)
print('%ds sleeping...' % diffTime)
log.info('%ds sleeping...' % diffTime)
if diffTime < 0.0:
print('W:input time is illegal')
raise RuntimeError('input time is illegal')
return
time.sleep(diffTime)
def shutDown(second):
if second > 7200:
print("W:", 'second time is too long')
print('[I]%d秒后关机' % second)
os.system(f"shutdown /s /t {second}")
def workFlow():
# 读取excel表格
data = xlrd.open_workbook(excelPath)
sheet = data.sheet_names() #获取sheet
# print(sheet)
table = data.sheet_by_name('Sheet1') #读取指定sheet
# print(table.nrows, table.ncols)
for rowIdx in range(table.nrows):
if rowIdx == 0 :
continue
row_val = table.row_values(rowIdx)
print(type(row_val), row_val)
log.info("row_val:%r" % row_val)
#flag=0 不需要开
if int(row_val[0]) == 0 :
continue
#默认打开商城
#搜索道具
runTime('Start')
imgX, imgY = getImgLoc('searchBox.png')
runTime('getImgLoc Cost:')
mouseClick(imgX, imgY, 3, 'left')
#搜索
if len(row_val) < 2 :
print("W:第%d行参数不够" % rowIdx)
continue
print('row_val[2]:', row_val[2])
pc.copy(row_val[2])
# pc.paste() #发现不能粘贴到搜索框
# pag.hotkey('ctrl','v') #不知道为什么不行
ctrlV()
pag.press('enter')
runTime('preOpen')
#打开箱子
imgX, imgY = getImgLoc(row_val[4])
mouseClick(imgX, imgY, 1, 'left')
runTime('endOpen')
#确认打开
okX, okY = getImgLoc('oK.png')
mouseClick(okX, okY, 1, 'left')
runTime('OK')
time.sleep(2)
#继续开启
runTime('pre continue')
openContinueX, openContinueY = getImgLoc('continueOpen.png')
log.info("imgX:%r,imgY:%r, okX:%r, okY:%r, oCX:%r, oCY:%r" % (imgX, imgY, okX, okY, openContinueX, openContinueY))
runTime('end continue')
print('开%d次' % int(row_val[3]))
#sleep到指定时间后,开始运行
t = [2024, 3, 5, 0, 0, 0]
sleepToDesignatedTime(t)
if openContinueX > 1 and openContinueY > 1:
#存在继续开启按钮
for timesIdx in range(int(row_val[3])):
log.info("Continue.start[%r]"%timesIdx)
mouseClick(openContinueX, openContinueY, 1, 'left')
# time.sleep(0.05)
log.info("Continue.end[%r]"%timesIdx)
# runTime('continue exists')
runTime('final')
else:
for timeIdx in range(int(row_val[3])):
log.info("noContinue.start[%r]"%timeIdx)
pag.press('enter')
mouseClick(imgX, imgY, 1, 'left') #打开箱子
mouseClick(okX, okY, 1, 'left') #确认打开
# time.sleep(0.05)
log.info("noContinue.end[%r]"%timeIdx)
# runTime('continue not exists')
runTime('final1')
#默认5秒后关机
ShutdownSecond = 5
shutDown(ShutdownSecond)
# pag.press('enter')
#获取按钮位置
if __name__ == "__main__":
workFlow()