Python学习笔记(十七)——用GUI自动化控制键盘鼠标

安装pyautogui模块

  • 在Windows上不需要安装其他模块
  • 在OS X上,运行
    sudo pip3 install pyobjc-framework-Quartz
    sudu pip3 install pyobhc-core
    sudo pip3 install pyobjc
  • 在linux上运行,
    sudo pip3 install python3-xlib,
    sudo apt-get install scrot,
    sudo apt-get install python3-tk
    sudo apt-get install python3-dev

通过注销关闭所有程序

  • Windows和linux 使用Ctrl + Alt + Del

暂停和启动故障防御装置

设置pyautogui.PAUSE = 1.5 每个GUI函数调用执行之后都会等在1.5秒

>>> import pyautogui
>>> pyautogui.PAUSE = 1
>>> pyautogui.FAILSAFE = True  #左上角的暂停函数

控制鼠标移动

>>> pyautogui.size()
(1366, 768)

移动鼠标

for i in range(10):
    pyautogui.moveRel(100,0,duration=0.25)
    pyautogui.moveRel(0,100,duration=0.25)
    pyautogui.moveRel(-100,0,duration=0.25)
    pyautogui.moveRel(0,-100,duration=0.25)
for i in range(10):
    pyautogui.moveRel(100,0,duration=0.25)
    pyautogui.moveRel(0,100,duration=0.25)
    pyautogui.moveRel(-100,0,duration=0.25)
    pyautogui.moveRel(0,-100,duration=0.25)

获取鼠标位置

>>> pyautogui.position()
(603, 641)

控制鼠标交互

点击鼠标

pyautogui.click(10,5,button='left')
pyautogui.click(10,5,button='middle')
pyautogui.click(10,5,button='right')

pyautogui.mouseDown(10,5,button='left')
pyautogui.mouseUp(10,5,button='left')
双击
pyautogui.doubleClick()
pyautogui.rightClick()
pyautogui.middleClick()

>>> import pyautogui
>>> pyautogui.click(10,5)

拖拽鼠标

 >>> distance = 200
>>> while distance > 0:
    pyautogui.dragRel(distance,0,duration=0.2)
    distance = distance -5
    pyautogui.dragRel(0,distance,duration=0.2)
    pyautogui.dragRel(-distance,0,duration=0.2)
    distance = distance -5
    pyautogui.dragRel(0,-distance,duration=0.2)

滑动鼠标

pyautogui.scroll(200) 想上滚动200

处理屏幕

获取屏幕快照

>>> im = pyautogui.screenshot()
>>> im.getpixel((0,0))
(67, 137, 212)

分析屏幕快照

>>> import pyautogui
>>> im = pyautogui.screenshot()
>>> im.getpixel((200,200))
(230, 230, 230)
>>> pyautogui.pixelMatchesColor(200,200,(230,230,230))
True
>>> pyautogui.pixelMatchesColor(200,200,(230,230,231))
False 

图像识别

>>> import pyautogui
>>> pyautogui.locateAllOnScreen('submit.png')
(643,754,70,29)
>>>list(pyautogui.locateAllOnScreen('submit.png'))
[(643,754,70,29),(1007,801,70,29)]
>>> pyautogui.center((643,754,70,29))
(678, 768)
>>> pyautogui.center((643,745,70,29))
(678, 759)
>>> pyautogui.click()

pyautogui.locateAllOnScreen('submit.png') 匹配屏幕快照中和submit.png 相同的图片

控制键盘

>>> pyautogui.click();pyautogui.typewrite('hello world!')
点击输入hello world!

键名

>>> pyautogui.click();pyautogui.typewrite(['a','b','left','left','X','Y'])

pyautogui
官方文档
Python学习笔记(十七)——用GUI自动化控制键盘鼠标_第1张图片
Python学习笔记(十七)——用GUI自动化控制键盘鼠标_第2张图片

按下和释放键盘

>>> pyautogui.keyDown('shift');pyautogui.press('4');pyautogui.keyUp('shift')
$

热键组合

pyautogui.hotkey('Ctrl','c')

复习

Python学习笔记(十七)——用GUI自动化控制键盘鼠标_第3张图片

自动填表程序

#! python3
#-*- encoding: utf-8 -*-# formFiller.py - 
# Usage:
#
# Author : qmeng
# MailTo : [email protected]
# QQ     : 1163306125
# Blog   : http://blog.csdn.net/Mq_Go/
# Create : 2018-02-20 13:27:48
# Version: 1.0
#
import pyautogui,time
# 建立坐标
nameField = (648, 319)
submitButton = (651, 817)
submitButtonColor = (75, 141, 249)
submitAnotherLink = (760, 224)
# 要填写的内容
formData = [{'name': 'Alice', 'fear': 'eavesdroppers', 'source': 'wand', 'robocop': 4, 'comments': 'Tell Bob I said hi.'},
            {'name': 'Bob', 'fear': 'bees', 'source': 'amulet', 'robocop': 4, 'comments': 'n/a'},
            {'name': 'Carol', 'fear': 'puppets', 'source': 'crystal ball', 'robocop': 1, 'comments': 'Please take the puppets out of the break room.'},
            {'name': 'Alex Murphy', 'fear': 'ED-209', 'source': 'money', 'robocop': 5, 'comments': 'Protect the innocent. Serve the public trust. Uphold the law.'},
            ]

#在每次Gui函数时停留0.5秒
pyautogui.PAUSE = 0.5

# 开始键入数据
for person in formData:
    print('>>> 5 SECOND PAUSE TO LET USER PRESS CTRL-C <<<')
    time.sleep(5)

    while not pyautogui.pixelMatchesColor(submitButton[0],submitButton[1],submitButtonColor):
        time.sleep(0.5)

    print('Entering %s info...' % (person['name']))
    pyautogui.click(nameField[0], nameField[1])

    # Fill out the Name field.
    pyautogui.typewrite(person['name'] + '\t')

    # Fill out the Greatest Fear(s) field.
    pyautogui.typewrite(person['fear'] + '\t')

    # 处理选项列表和单选按钮
    if person['source'] == 'wand':
        pyautogui.typewrite(['down', '\t'])
    elif person['source'] == 'amulet':
        pyautogui.typewrite(['down', 'down', '\t'])
    elif person['source'] == 'crystal ball':
        pyautogui.typewrite(['down', 'down', 'down', '\t'])
    elif person['source'] == 'money':
        pyautogui.typewrite(['down', 'down', 'down', 'down', '\t'])

    if person['robocop'] == 1:
        pyautogui.typewrite([' ', '\t'])
    elif person['robocop'] == 2:
        pyautogui.typewrite(['right', '\t'])
    elif person['robocop'] == 3:
        pyautogui.typewrite(['right', 'right', '\t'])
    elif person['robocop'] == 4:
        pyautogui.typewrite(['right', 'right', 'right', '\t'])
    elif person['robocop'] == 5:
        pyautogui.typewrite(['right', 'right', 'right', 'right', '\t'])

    pyautogui.typewrite(person['comments'] + '\t')
    pyautogui.press('enter')
    print('Clicked Submit.')
    time.sleep(5)
    pyautogui.click(submitAnotherLink[0], submitAnotherLink[1])

你可能感兴趣的:(Python)