c/s结构的自动化——pyautogui

环境:Python 3.5.3

pip install pyautogui -i http://pypi.douban.com/simple --trusted-host pypi.douban.com


import pyautogui as pag
# https://blog.csdn.net/astroboythu/article/details/78068070
# https://www.cnblogs.com/qq21270/p/7705008.html

# 获取屏幕分辨率
rec = screenWidth, screenHeight = pag.size()

# 截屏功能
img = pag.screenshot()
img.save('foo.png')

# 截取部分屏幕(x,y,宽度,长度)
img = pag.screenshot(region=(0, 0, 300,400))
img.save('foo1.png')

# 获得某个坐标的像素
(r,g,b) = img.getpixel((50, 200))
print(img.getpixel((50, 200)))

# 根据图片获取位置
pic_option = pag.locateOnScreen('sysfix.PNG')# (390, 58, 54, 64)
# 获取位置(中心点)点击
x, y = pag.center(rec) # 获得中心点
pag.click(x, y)
# 注释:返回None并报错:像素不匹配;在点击状态上再点击

# 检查XY坐标是否在屏幕上
pag.onScreen(0, 0) #True
pag.onScreen(0, -1) #False

# 鼠标操作
# 获取当前鼠标的坐标(left’, ‘middle’, ‘right’)
currentMouseX, currentMouseY = pag.position()
pag.click(pag.position(),button='right')

# 按键按下
pag.mouseDown(x=100, y=100, button='left')
# 按键松开
pag.mouseUp(x=100, y=100, button='left')
# 双击
pag.doubleClick(x=100, y=100)
# 右击
pag.rightClick(x=1000, y=100)
pag.click(x=1000, y=100,button='right')
# 中击
pag.middleClick(x=1000, y=100)

# 鼠标移动
pag.moveTo(100,100,duration=1) # 绝对坐标
pag.moveRel(100,100,duration=1) # 相对当前操作坐标

# 鼠标拖拽(duration时间不能太短,拖动太快有些系统会吃不消)
pag.dragTo(x=583,y=590,duration=3)
pag.dragRel(x,y,duration=2)

# 双击左键
pag.click(pag.position(),clicks=2)
# 两次单击之间停留0.25秒
pag.click(clicks=2, interval=2)
# 三击右键
pag.click(button='right', clicks=2, interval=0.25)

# 滚轮
pag.scroll(clicks=2,x=435,y=417)


# pyautogui.easeInQuad()函数可以用于moveTo(),moveRel(),dragTo()和dragRel()函数
# 开始很慢,不断加速
pag.moveTo(100, 100, 2, pag.easeInQuad)
# 开始很快,不断减速
pag.moveTo(100, 100, 2, pag.easeOutQuad)
# 开始和结束都快,中间比较慢
pag.moveTo(100, 100, 2, pag.easeInOutQuad)
# 一步一徘徊前进
pag.moveTo(100, 100, 2, pag.easeInBounce)
# 徘徊幅度更大,甚至超过起点和终点
pag.moveTo(100, 100, 2, pag.easeInElastic)


# 消息弹窗函数
pag.alert('这个消息弹窗是文字+OK按钮')
pag.confirm('这个消息弹窗是文字+OK+Cancel按钮')
pag.prompt('这个消息弹窗是让用户输入字符串,单击OK')
#返回用户输入的字符串,如果用户什么都不输入,则返回None

# 默认延迟时间是2.5秒
pag.PAUSE = 2.5
# 键盘操作
# 输入字符串
pag.typewrite('Hello world',interval=0.25)

PyAutoGUI键盘表:

字符串 代表按键
‘enter’(或‘return’ 或 ‘\n’) 回车
‘esc’ ESC键
‘shiftleft’, ‘shiftright’ 左右SHIFT键
‘altleft’, ‘altright’ 左右ALT键
‘ctrlleft’, ‘ctrlright’ 左右CTRL键
‘tab’ (‘\t’) TAB键
‘backspace’, ‘delete’ BACKSPACE 、DELETE键
‘pageup’, ‘pagedown’ PAGE UP 和 PAGE DOWN键
‘home’, ‘end’ HOME 和 END键
‘up’, ‘down’, ‘left’,‘right’ 箭头键
‘f1’, ‘f2’, ‘f3’…. F1…….F12键
‘volumemute’, ‘volumedown’,‘volumeup’ 有些键盘没有
‘pause’ PAUSE键
‘capslock’, ‘numlock’,‘scrolllock’ CAPS LOCK, NUM LOCK, 和 SCROLLLOCK 键
‘insert’ INS或INSERT键
‘printscreen’ PRTSC 或 PRINT SCREEN键
‘winleft’, ‘winright’ Win键
‘command’ Mac OS X command键



  • keyDown():按下某个键
  • keyUp():松开某个键
  • press():一次完整的击键,前面两个函数的组合。
  • hotkey(‘ctrl’,’c’):热键函数

 

例子:

 

import pyautogui as pag

img = pag.screenshot(region=(0, 0, 500,500))
img.save('foo2.png')
pic_option = pag.locateOnScreen('foo2.png')
rec = pag.center(pic_option)

a = (rec[0],rec[1])
g = (a[0],a[1]+200)
b = (a[0]+30,a[1]+10)
c = (b[0],b[1]+30)
d = (a[0],g[1]//2)
e = (b[0],(g[1]//2)+10)
f = (b[0],int(g[1]-10))

pag.mouseDown(x=a[0], y=a[1])
pag.moveTo()
pag.mouseUp(g[0], g[1])

pag.mouseDown(a[0], a[1])
pag.moveTo()
pag.mouseUp(b[0], b[1])

pag.mouseDown(b[0], b[1])
pag.moveTo()
pag.mouseUp(c[0], c[1])

pag.mouseDown(c[0], c[1])
pag.moveTo()
pag.mouseUp(a[0], a[0]+50)


pag.mouseDown(a[0], a[0]+50)
pag.moveTo()
pag.mouseUp(b[0], a[0]+60)

pag.mouseDown(b[0], a[0]+60)
pag.moveTo()
pag.mouseUp(f[0], f[1])

pag.mouseDown(f[0], f[1])
pag.moveTo()
pag.mouseUp(g[0], g[1])

 

转载于:https://www.cnblogs.com/zhang-dan/p/9464071.html

你可能感兴趣的:(c/s结构的自动化——pyautogui)