美文网首页
【Python】模拟鼠标操作

【Python】模拟鼠标操作

作者: 嘻洋洋 | 来源:发表于2019-05-20 17:28 被阅读0次

    介绍两种方法

    1.使用pymouse模拟鼠标操作

    (1)安装过程

    • 支撑插件
      下载pywin32-221.win-amd64-py3.6.exe,然后安装
      安装离线插件pip install pyHook-1.5.1-cp36-cp36m-win_amd64.xiwhl
    • pymouse插件
      现在安装pip install PyMouse。安装后Python\Python36\Lib\site-packages\pymouse目录下init.py:找到第92行的windows将其改写成 pymouse.windows

    (2)常见操作

    • 移动
    m.move(x,y)#鼠标移动到xy位置
    
    • 点击
    m.click(x,y)#移动并且在xy位置点击
    m.click(x,y,1|2)#移动并且在xy位置点击,左右键点击
    

    详细代码

    from io import BytesIO
    import sys,importlib
    from urllib import parse
    import json 
    from pymouse import PyMouse
    import time
    
    #SerializerStr = parse.unquote(sys.argv[1])
    SerializerStr = '{"X1":"682","Y1":"342","X2":"707","Y2":"358","X3":"562","Y3":"364"}'
    PluginArray = json.loads(SerializerStr)
    X1 = int(PluginArray["X1"]) # 需要强制转换int
    Y1 = int(PluginArray["Y1"])
    X2 = int(PluginArray["X2"])
    Y2 = int(PluginArray["Y2"])
    X3 = int(PluginArray["X3"])
    Y3 = int(PluginArray["Y3"])
    time.sleep(5)
    m = PyMouse()
    #m.position()#获取当前坐标的位置
    m.move(670,444)
    time.sleep(3)
    m.move(X1,Y1)
    time.sleep(4)
    m.click(X1,Y1,2)
    time.sleep(3) # 3秒
    m.click(X2,Y2)
    time.sleep(2)
    m.click(X3,Y3)
    print(X1,Y1,X2,Y2,X3,Y3)
    #target_link = PluginArray["imgarcone"];
    #template_link =  PluginArray["imgarctwo"]; 
    #test1 = '{"name": "xiaoming"}' # 双引号和单引号不要搞错了。
    #PluginArray = json.loads(test1)
    #'{"X1":"300","Y1":"300","X2":"230","Y2":"400","X3":"200","Y3":"300"}'
    
    

    2.使用pyautogui模拟鼠标操作

    (1)安装两个插件
    pip install PyGetWindow==0.0.1
    pip install pyautogui
    (2)常见操作

    • 移动
    # 鼠标经过1秒的时间,移动到了(100,100)的位置
    pyautogui.moveTo(100,100,duration = 1)
    pyautogui.moveTo(100, 200) #绝对移动
    pyautogui.moveRel(100, 200)#相对移动
    #渐变移动
    pyautogui.moveTo(100, 100, 2, pyautogui.easeInQuad)     # start slow, end fast
    pyautogui.moveTo(100, 100, 2, pyautogui.easeOutQuad)    # start fast, end slow
    pyautogui.moveTo(100, 100, 2, pyautogui.easeInOutQuad)  # start and end fast, slow in middle
    pyautogui.moveTo(100, 100, 2, pyautogui.easeInBounce)   # bounce at the end
    pyautogui.moveTo(100, 100, 2, pyautogui.easeInElastic)  # rubber band at the end
    

    +鼠标拖拽

    pyautogui.dragTo(100, 200, button='left')#绝对移动
    pyautogui.dragRel(30, 0, 2, button='right') #相对移动
    pyautogui.scroll(10, x=100, y=100)  # move mouse cursor to 100, 200, then scroll up 10 "clicks"
    
    • 鼠标点击
    pyautogui.click(10,5)           # 在(10,5)单击鼠标,默认左键
    pyautogui.click(100,150,button='left')
    pyautogui.click(200,250,button='right')
    # pyautogui.dragTo()  按键并拖动鼠标移动,参数为坐标,与moveTo相同
    # pyautogui.dragRel()  按键并拖动鼠标移动,参数为距离,与moveRel相
    #鼠标按下
    pyautogui.mouseDown()
    pyautogui.mouseUp()
    同
    

    详细代码

    from urllib import parse
    import json 
    import pyautogui
    import time
    
    #SerializerStr = parse.unquote(sys.argv[1])
    SerializerStr = '{"X1":"771","Y1":"319","X2":"660","Y2":"324","X3":"582","Y3":"317"}'
    #转换成json对象
    PluginArray = json.loads(SerializerStr)
    X1 = int(PluginArray["X1"]) # 需要强制转换int
    Y1 = int(PluginArray["Y1"])
    X2 = int(PluginArray["X2"])
    Y2 = int(PluginArray["Y2"])
    X3 = int(PluginArray["X3"])
    Y3 = int(PluginArray["Y3"])
    time.sleep(5)
    pyautogui.position() 
    pyautogui.moveTo(670,444)
    time.sleep(3)
    pyautogui.moveTo(X1,Y1,duration = 0.4)
    time.sleep(1)
    pyautogui.click(X1,Y1)
    time.sleep(1)
    pyautogui.moveTo(670,444)
    time.sleep(3)
    pyautogui.moveTo(X2,Y2,duration = 0.4)
    time.sleep(2) # 3秒
    pyautogui.click(X2,Y2)
    time.sleep(1)
    pyautogui.moveTo(670,444)
    time.sleep(3)
    
    pyautogui.moveTo(X3,Y3,duration = 0.4)
    time.sleep(2)
    pyautogui.click(X3,Y3)
    print(X1,Y1,X2,Y2,X3,Y3)
    #target_link = PluginArray["imgarcone"];
    #template_link =  PluginArray["imgarctwo"]; 
    #test1 = '{"name": "xiaoming"}' # 双引号和单引号不要搞错了。
    #PluginArray = json.loads(test1)
    #'{"X1":"300","Y1":"300","X2":"230","Y2":"400","X3":"200","Y3":"300"}'
    

    模拟鼠标操作

    import sys
    from urllib import parse
    import json 
    import pyautogui
    import time
    
    #SerializerStr = parse.unquote(sys.argv[1])
    SerializerStr = '{"X1":"667","Y1":"338","X2":"746","Y2":"291","X3":"562","Y3":"303"}'
    PluginArray = json.loads(SerializerStr)
    X1 = int(PluginArray["X1"]) # 需要强制转换int
    Y1 = int(PluginArray["Y1"])
    X2 = int(PluginArray["X2"])
    Y2 = int(PluginArray["Y2"])
    X3 = int(PluginArray["X3"])
    Y3 = int(PluginArray["Y3"])
    time.sleep(5)
    pyautogui.position() 
    pyautogui.moveTo(670,444)
    time.sleep(3)
    pyautogui.moveTo(X1,Y1,1,pyautogui.easeInOutQuad)
    time.sleep(1)
    pyautogui.mouseDown()
    pyautogui.mouseUp()
    time.sleep(1)
    pyautogui.moveTo(X2,Y2,0.8,pyautogui.easeInOutQuad)
    time.sleep(2) # 3秒
    pyautogui.mouseDown()
    pyautogui.mouseUp()
    time.sleep(1)
    pyautogui.moveTo(X3,Y3,0.6,pyautogui.easeInOutQuad)
    time.sleep(2)
    pyautogui.mouseDown()
    pyautogui.mouseUp()
    time.sleep(2)
    print(X1,Y1,X2,Y2,X3,Y3)
    #target_link = PluginArray["imgarcone"];
    #template_link =  PluginArray["imgarctwo"]; 
    #test1 = '{"name": "xiaoming"}' # 双引号和单引号不要搞错了。
    #PluginArray = json.loads(test1)
    #'{"X1":"300","Y1":"300","X2":"230","Y2":"400","X3":"200","Y3":"300"}'
    

    其它:还有截屏、消息框和键盘操作等功能

    相关文章

      网友评论

          本文标题:【Python】模拟鼠标操作

          本文链接:https://www.haomeiwen.com/subject/zrjsmqtx.html