python PyAutoGUI 模擬鼠標(biāo)鍵盤操作和截屏功能
簡(jiǎn)介
一款跨平臺(tái)/無(wú)依賴的自動(dòng)化測(cè)試工具,目測(cè)只能控制鼠標(biāo)/鍵盤/獲取屏幕尺寸/彈出消息框/截屏。
安裝
pip install pyautogui
鼠標(biāo)鍵盤控制
>>> import pyautogui
>>> screenWidth, screenHeight = pyautogui.size()
>>> currentMouseX, currentMouseY = pyautogui.position()
>>> pyautogui.moveTo(100, 150)
>>> pyautogui.click()
>>> pyautogui.moveRel(None, 10) # move mouse 10 pixels down
>>> pyautogui.doubleClick()
>>> pyautogui.moveTo(500, 500, duration=2, tween=pyautogui.tweens.easeInOutQuad) # use tweening/easing function to move mouse over 2 seconds.
>>> pyautogui.typewrite('Hello world!', interval=0.25) # type with quarter-second pause in between each key
>>> pyautogui.press('esc')
>>> pyautogui.keyDown('shift')
>>> pyautogui.typewrite(['left', 'left', 'left', 'left', 'left', 'left'])
>>> pyautogui.keyUp('shift')
>>> pyautogui.hotkey('ctrl', 'c')
顯示消息彈出框
>>> import pyautogui
>>> pyautogui.alert('This is an alert box.')
'OK'
>>> pyautogui.confirm('Shall I proceed?')
'Cancel'
>>> pyautogui.confirm('Enter option.', buttons=['A', 'B', 'C'])
'B'
>>> pyautogui.prompt('What is your name?')
'Al'
>>> pyautogui.password('Enter password (text will be hidden)')
'swordfish'
截屏
>>> import pyautogui
>>> im1 = pyautogui.screenshot()
>>> im1.save('my_screenshot.png')
>>> im2 = pyautogui.screenshot('my_screenshot2.png')
定位截屏
>>> import pyautogui
>>> button7location = pyautogui.locateOnScreen('button.png') # returns (left, top, width, height) of matching region
>>> button7location
(1416, 562, 50, 41)
>>> buttonx, buttony = pyautogui.center(button7location)
>>> buttonx, buttony
(1441, 582)
>>> pyautogui.click(buttonx, buttony) # clicks the center of where the button was found
參考
http://pyautogui.readthedocs.io/en/latest/index.html
https://github.com/asweigart/pyautogui
https://github.com/asweigart/sushigoroundbot
總結(jié)
以上所述是小編給大家介紹的python PyAutoGUI 模擬鼠標(biāo)鍵盤操作和截屏功能,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)歡迎給我留言,小編會(huì)及時(shí)回復(fù)大家的!
相關(guān)文章
PyCharm設(shè)置SSH遠(yuǎn)程調(diào)試的方法
這篇文章主要介紹了PyCharm設(shè)置SSH遠(yuǎn)程調(diào)試的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-07-07
Python操作JSON文件的知識(shí)點(diǎn)整理
Python?提供了內(nèi)置的?json?模塊來(lái)處理?JSON?格式的文件。該模塊主要分為讀取和寫入?JSON?文件。本文主要為大家整理了一些Python操作JSON文件的知識(shí)點(diǎn),需要的可以參考一下2023-01-01
讓你的Python代碼實(shí)現(xiàn)類型提示功能
今天小編就為大家分享一篇讓你的Python代碼實(shí)現(xiàn)類型提示功能,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-11-11
Appium+python+unittest搭建UI自動(dòng)化框架的實(shí)現(xiàn)
本文主要介紹了Appium+python+unittest搭建UI自動(dòng)化框架的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2024-03-03
Win10下用Anaconda安裝TensorFlow(圖文教程)
這篇文章主要介紹了Win10下用Anaconda安裝TensorFlow(圖文教程),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06
PyCharm MySQL可視化Database配置過(guò)程圖解
這篇文章主要介紹了PyCharm MySQL可視化Database配置過(guò)程圖解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-06-06

