tags: pyautogui
用了一下 pyautogui
這個自動化套件,
覺得這個套件其實還蠻容易上手的,
你可以把它想成某種程式化的「按鍵精靈」
會照著你的想法去操作鍵盤、滑鼠。
在製作 RPA (Robotic Process Automation) 節省時間,增進效率,
會是一個非常關鍵要件
這邊帶你快速上手 pyautogui,算是我的某種 Cheat Sheet 吧
PyAutoGUI 介紹
PyAutoGUI 是一個用於 桌面自動化 的 Python 套件,能模擬滑鼠移動、點擊、拖曳,以及鍵盤輸入等操作。它跨平台支援 Windows、macOS、Linux,非常適合用來撰寫腳本自動完成重複性工作,例如批次截圖、測試 UI 或自動填表。PyAutoGUI 也提供螢幕截圖與簡易畫面影像辨識功能,能根據畫面上的元素定位與操作,讓自動化更靈活易用。
快速上手 pyautogui
鍵盤滑鼠類
列出我幾個常用的 method
按組合鍵
pyautoui.hotkey('win', 'r')
鍵盤上按指定按鍵
pyautogui.press('tab')
鍵盤打字
pyautogui.write('Hello, World.')
把滑鼠移過去點擊
pyautogui.click(100, 50, duration=0.5)
螢幕相關
找圖片上的位置(做定位點)
myAncher = pyautogui.locateOnScreen('button.png', grayscale=True)
螢幕截圖
pyautogui.screenshot('screenshot/1.png')
這邊你可以先螢幕截圖到時候讓程式來便是它
如果找不到圖片會噴 ImageNotFoundException
光這樣就可以玩很多花樣了
警告視窗系列
其他的部分它有簡單的提供一些 Alert, Confirm 的視窗
打開一個 Alert 警告窗
pyautogui.alert('This displays some text with an OK button.')
有時候提示使用者需要用到
取得視窗相關
取得所有視窗物件
pyautogui.getAllWindows()
取得所有視窗標題
pyautogui.getAllTitles()
它官網範例是操作 Windows 計算機
準備各按鈕截圖好的圖片,利用 locateOnScreen()
來定位
用 click()
來點擊操作計算機
基礎是這樣
參考資料
https://ithelp.ithome.com.tw/articles/10277668
https://pyautogui.readthedocs.io/en/latest/