­

詳解pyautogui模塊

一.安裝

pip install pyautogui

或者

pip install -i  //pypi.tuna.tsinghua.edu.cn/simple pyautogui

二.全局設置

s為秒數,不填默認0.1s。表示所有pyautogui相關的指令運行後等待s秒在運行下一條。相當於全局等待

pyautogui.PAUSE=s

防止操作失控,默認就是True,通過FAILSAFE_POINTS來設置坐標,默認(0,0)

也可手動更改

pyautogui.FAILSAFE = True  # 防止操作失控,設置鼠標移動某點是拋異常
pyautogui.FAILSAFE_POINTS = [(100, 0)] # 鼠標移動到(100,0)位置時拋出異常

三.獲取電腦分辨率

x, y = pyautogui.size()
print(x, y)

四.鼠標事件

1.pyautogui.click():單擊點擊事件

pyautogui.click(x=None, y=None, clicks=1, interval=0.0, button=PRIMARY, duration=0.0, tween=linear)
# x,y:為點擊的位置坐標,以電腦左上角為原點
# clicks:為點擊次數,默認為1
# interval:多次點擊間的間隔(比如雙擊,第一次點擊完後,等待設置的秒數在執行下一次點擊)
# button:設置左擊還是右擊,默認左擊
# duration:設置執行的時間
# tween:鼠標移動到指定位置的規律

button:設置鼠標點擊項(left:左擊 ,right:右擊 ,middle:中擊)

除了click()方法可以單擊外,pyautogui還提供了專用於左/右/中擊的方法。

pyautogui.leftClick()  # 左鍵單擊
pyautogui.rightClick()  # 單擊右鍵
pyautogui.middleClick()  # 中鍵單擊

leftClick()的用法等同於click(button=’left’)

rightClick()的用法等同於click(button=’right’)

middleClick()的用法等同於click(button=’middle’)

2.pyautogui.doubleClick():雙擊點擊事件

pyautogui.doubleClick()  # 雙擊,默認左雙擊
# x,y:為點擊的位置坐標,以電腦左上角為原點
# clicks:為點擊次數,默認為1
# interval:兩次點擊間的間隔(比如雙擊,第一次點擊完後,等待設置的秒數在執行下一次點擊)
# button:設置左擊還是右擊,默認左擊
# duration:設置執行的時間
# tween:鼠標移動到指定位置的規律

3.pyautogui.tripleClick():三擊點擊事件

pyautogui.tripleClick()  # 單擊三次,默認左擊

4.鼠標移動

1)pyautogui.moveTo():以坐標為原點移動到指定的坐標上

pyautogui.moveTo(x=None, y=None, duration=0.0, tween=pyautogui.easeInOutElastic)  # 鼠標移動到坐標位置,以電腦屏幕左上角為原地定位
# x,y:為移動的位置坐標,以電腦左上角為原點
# button:設置左擊還是右擊,默認左擊
# duration:設置執行的時間
# tween:鼠標移動到指定位置的規律,默認勻速直線運行(linear)

這個介紹下tween參數的值共有哪些。

easeInQuad 

 

easeOutQuad 
easeInOutQuad 
easeInCubic 
easeOutCubic 
easeInOutCubic 
easeInQuart 
easeOutQuart 
easeInOutQuart 
easeInQuint 
easeOutQuint 
easeInOutQuint 
easeInSine 
easeOutSine 
easeInOutSine 
easeInExpo 
easeOutExpo 
easeInOutExpo 
easeInCirc 
easeOutCirc 
easeInOutCirc 
easeInElastic 
easeOutElastic 
easeInOutElastic 
easeInBack 
easeOutBack 
easeInOutBack 
easeInBounce 
easeOutBounce 
easeInOutBounce 

左邊為屬性,右側為運行的方式(速度+軌跡),圖片來源於』//blog.csdn.net/S_clifftop?type=blog『如有侵權,請告知刪除。

pyautogui.position():可獲取當前鼠標坐標

x, y = pyautogui.position()  # 獲取鼠標當前位置
print(x,y)

2)pyautogui.move():以鼠標當前位置移動x,y個像素

pyautogui.move(x=None, y=None, duration=0.0, tween=pyautogui.easeInQuad)  # 在當前位置移動,以當前位置為坐標

該方法與moveTo()的區別在於開始移動的位置不一樣,x,y參數是以當前位置相對移動的像素,可正值可負值。

5.按下與鬆開

pyautogui.mouseDown():按下鼠標

pyautogui.mouseDown(x=None, y=None, button='', duration=0.0)  # 按下鼠標,填寫坐標的話,就是移動到某個坐標下按下
# x,y為移動到某點按下,不填的話默認當前位置按下
# button按下的點擊項,默認左按下
# duration整體指令運行的時間

pyautogui.mouseUp():鬆開鼠標

pyautogui.mouseUp()  # 釋放鼠標

我們在做登錄驗證滑塊的時候,就可以用這兩個方法+move()去拖拽滑塊,並且還不會被檢測出來

6.鼠標拖拽

pyautogui.dragRel():在鼠標當前位置點擊後相對移動

pyautogui.dragRel(xOffset=20, yOffset=100, duration=3, tween=pyautogui.easeInQuad, button='left')  # 在鼠標當前位置點擊後相對移動,結束後自動鬆開
# xOffset,yOffset為移動的距離
# duration整個移動的耗時
# tween移動的速度+軌跡
# button設置點擊項,默認左擊

pyautogui.drag():該方法與dragRel()用法及效果一致,這裡不做多餘介紹了

pyautogui.dragTo():從當前位置拖拽到指定坐標軸

pyautogui.dragTo(x=None, y=None, duration=0.0, tween=pyautogui.easeInQuad, button="left")  # 鼠標從當前位置拖拽到指定坐標
# x,y為坐標,鼠標移動到指定的坐標(x,y)
# duration整個移動的耗時
# tween移動的速度+軌跡
# button設置點擊項,默認左擊

五.鍵盤事件

1.按下與鬆開

pyautogui.keyDown(”) :按下某鍵盤

pyautogui.keyDown('shift')  # 按下shift鍵

pyautogui.keyUp(”) :鬆開某鍵盤

pyautogui.keyUp('shift')  # 鬆開shift鍵

常用的鍵盤表示(自己腦補用法吧)

KEY_NAMES = [
    "\t", # tab
    "\n",  # 回車
    "\r",
    " ",
    "!",
    '"',
    "#",
    "$",
    "%",
    "&",
    "'",
    "(",
    ")",
    "*",
    "+",
    ",",
    "-",
    ".",
    "/",
    "0",
    "1",
    "2",
    "3",
    "4",
    "5",
    "6",
    "7",
    "8",
    "9",
    ":",
    ";",
    "<",
    "=",
    ">",
    "?",
    "@",
    "[",
    "\\",
    "]",
    "^",
    "_",
    "`",
    "a",
    "b",
    "c",
    "d",
    "e",
    "f",
    "g",
    "h",
    "i",
    "j",
    "k",
    "l",
    "m",
    "n",
    "o",
    "p",
    "q",
    "r",
    "s",
    "t",
    "u",
    "v",
    "w",
    "x",
    "y",
    "z",
    "{",
    "|",
    "}",
    "~",
    "accept",
    "add",
    "alt",
    "altleft",
    "altright",
    "apps",
    "backspace",
    "browserback",
    "browserfavorites",
    "browserforward",
    "browserhome",
    "browserrefresh",
    "browsersearch",
    "browserstop",
    "capslock",
    "clear",
    "convert",
    "ctrl",
    "ctrlleft",
    "ctrlright",
    "decimal",
    "del",
    "delete",
    "divide",
    "down",
    "end",
    "enter",
    "esc",
    "escape",
    "execute",
    "f1",
    "f10",
    "f11",
    "f12",
    "f13",
    "f14",
    "f15",
    "f16",
    "f17",
    "f18",
    "f19",
    "f2",
    "f20",
    "f21",
    "f22",
    "f23",
    "f24",
    "f3",
    "f4",
    "f5",
    "f6",
    "f7",
    "f8",
    "f9",
    "final",
    "fn",
    "hanguel",
    "hangul",
    "hanja",
    "help",
    "home",
    "insert",
    "junja",
    "kana",
    "kanji",
    "launchapp1",
    "launchapp2",
    "launchmail",
    "launchmediaselect",
    "left",
    "modechange",
    "multiply",
    "nexttrack",
    "nonconvert",
    "num0",
    "num1",
    "num2",
    "num3",
    "num4",
    "num5",
    "num6",
    "num7",
    "num8",
    "num9",
    "numlock",
    "pagedown",
    "pageup",
    "pause",
    "pgdn",
    "pgup",
    "playpause",
    "prevtrack",
    "print",
    "printscreen",
    "prntscrn",
    "prtsc",
    "prtscr",
    "return",
    "right",
    "scrolllock",
    "select",
    "separator",
    "shift",
    "shiftleft",
    "shiftright",
    "sleep",
    "space",
    "stop",
    "subtract",
    "tab",
    "up",
    "volumedown",
    "volumemute",
    "volumeup",
    "win",
    "winleft",
    "winright",
    "yen",
    "command",
    "option",
    "optionleft",
    "optionright",
]

2.輸入內容

pyautogui.press(‘ ‘):輸入內容

pyautogui.keyDown('shift')  # 按下shift鍵
pyautogui.press("a")  # 打印A
pyautogui.keyUp('shift')  # 鬆開shift鍵

3.緩慢輸入

pyautogui.typewrite():緩慢輸入

pyautogui.typewrite("message", interval=2)  # 緩慢輸入
# message:為輸入的內容
# interval:設置每兩個字符輸入的時間間隔,單位s

4.多鍵組合使用

pyautogui.hotkey():多鍵組合使用(如以下設置全選)

pyautogui.hotkey('ctrl', 'a')  # 多鍵組合使用

六.彈框

pyautogui.alert():alert彈框

a = pyautogui.alert(text='test', title='pyautogui')  # alert彈框
# text:彈框內容
# title:彈框標題
print(a)  # 打印ok

當彈框運行時程序會暫停往下走,點擊取消/確定時才會繼續往下執行

pyautogui.confirm():confirm彈框

a = pyautogui.confirm(text='test', title='pyautogui')  # alert彈框,點擊確定返回ok,點擊取消返回cancel
# text:彈框內容
# title:彈框標題
print(a)

pyautogui.password():password彈框

pyautogui.prompt():prompt彈框

七.截圖

pyautogui.screenshot():截圖

img = pyautogui.screenshot()  # 默認截取全屏
img.save("./img/gui.png")  # 截取後的圖片保存位置

如果我們想要截取某坐標的圖片,可以添加region元組

region = (1690, 144, 100, 20)  # 前兩位為x,y坐標,第三位為截取圖片的寬,第四位為截取圖片的高
img = pyautogui.screenshot(region=region)  
img.save("./img/gui.png")

pyautogui.locateCenterOnScreen():根據圖片獲取在當前屏幕匹配的第一個中心坐標

x, y = pyautogui.locateCenterOnScreen("./img/gui.png")
print(x,y)  # 打印匹配的x,y坐標

若無匹配結果,則返回None,報cannot unpack non-iterable NoneType object異常

除了locateCenterOnScreen(),還有以下用法也可獲取匹配圖片坐標

a = pyautogui.locateOnScreen("./img/gui.png")  # 返回匹配圖片的左上角坐標+寬+高
x, y = pyautogui.center(a)  # 或者中心坐標位置
print(x, y)

pyautogui.locateAllOnScreen():根據圖片獲取在當前屏幕匹配的所有坐標及寬高

img = pyautogui.locateAllOnScreen("./img/gui.png")  # 返回頁面所有可匹配的圖片,返回一個生成器(左,頂,寬,高)
for i in img:
    x, y, w, h = i
    print(x, y)

這裡需要注意的是,返回的結果是一個生成器,需for遍歷輸出

八.頁面滾動

pyautogui.scroll():垂直滾動,負數為向下滾動,正數為向上滾動

pyautogui.scroll(-500)

如果想要緩慢滾動,可以加個for循環

for i in range(1, 100):
    pyautogui.scroll(-i) 

pyautogui.hscroll() :水平滾動

 這個不做多餘介紹了,只有linux系統支持

pyautogui.vscroll() :垂直滾動,負下正上

pyautogui.vscroll(-100)  # 垂直滾動,負下正

















文章來源://www.cnblogs.com/lihongtaoya/ ,請勿轉載
Tags: