每天手动登录fortClient好麻烦,就想写一个按键精灵帮我登录,趁机泡杯咖啡它不香吗~
使用时发现等待时间的不可控性太大了,有的时候程序没启动起来就进行后续点击动作,导致整个程序乱掉。于是改用库中locateOnScreen()来实时查看屏幕当前情况。
import pyautogui
def findElement(a):
buttonlocation = None
while buttonlocation == None:
try:
buttonlocation = pyautogui.locateOnScreen(a, confidence=0.8)
except:
continue
return buttonlocation
def Login():
# 获取当前屏幕分辨率
screenWidth, screenHeight = pyautogui.size()
print(screenWidth, screenHeight)
# 获取当前鼠标位置
currentMouseX, currentMouseY = pyautogui.position()
print(currentMouseX, currentMouseY)
#---------- 切换输入法 ----------#
pyautogui.PAUSE = 0.5
pyautogui.click(x=1142, y=11, duration=0.25)
pyautogui.click(x=1142, y=104, duration=0.25)
#---------- 打开fortclient ----------#
pyautogui.hotkey('option', 'command', 'p')
findElement('search.png')
pyautogui.click(x=720, y=41, duration=0.25)
pyautogui.typewrite(message="fort")
findElement('fort.png')
pyautogui.click(x=720, y=120, duration=0.25)
#---------- 点击登录 ----------#
findElement('btn.png')
pyautogui.click(x=721, y=605, duration=0.25)
#---------- 输入账号密码 ----------#
findElement('next.png')
pyautogui.typewrite(message="账号")
pyautogui.press('enter')
findElement('log.png')
pyautogui.typewrite(message="密码")
pyautogui.press('enter')
findElement('yes.png')
pyautogui.press('enter')
if __name__ == "__main__":
Login()
提供了很多好用的键鼠自动控制API。
使用 $ pip3 install pyautogui
指令下载该库。
import pyautogui
def Login():
# 获取当前屏幕分辨率
# screenWidth, screenHeight = pyautogui.size()
# 获取当前鼠标位置
# currentMouseX, currentMouseY = pyautogui.position()
#---------- 打开fortclient ----------#
pyautogui.PAUSE = 1
pyautogui.hotkey('option', 'command', 'p')
pyautogui.write('fort')
pyautogui.PAUSE = 3.5
pyautogui.click(x=720, y=120, duration=0.25)
#---------- 点击登录 ----------#
pyautogui.click(x=721, y=605, duration=0.25)
pyautogui.PAUSE = 1.5
#---------- 输入账号密码 ----------#
pyautogui.typewrite(message="账号")
pyautogui.press('enter')
pyautogui.typewrite(message="密码")
pyautogui.press('enter')
pyautogui.press('enter')
if __name__ == "__main__":
Login()
几个实现功能的小窍门:
更多关于pyautogui库的使用方法请参考pyautogui官方指南。