python模拟鼠标键盘操作 GhostMouse tinytask 调用外部脚本或程序 autopy右键另存为

0.关键实现:程序窗口前置

python 通过js控制滚动条拉取全文 通过psutil获取pid窗口句柄,通过win32gui使程序窗口前置 通过pyauto实现右键菜单和另存为操作

1.参考

  • autopy (实践见最后一章节)

用Python制作游戏外挂(上)

AutoPy Introduction and Tutorial

autopy.mouse.smooth_move(1, 1) 可以实现平滑移动

autopy — API Reference

  • pip install PyUserInput

SavinaRoja/PyUserInput

[python3.5][PyUserInput]模拟鼠标和键盘模拟

Python-模拟鼠标键盘动作

  • autoit

selenium借助AutoIt识别上传(下载)详解

selenium webdriver 右键另存为下载文件(结合robot and autoIt)

  • win32api

Python实现windows下模拟按键和鼠标点击的方法

用Python模拟键盘输入

  •  pyautowin

https://pyautogui.readthedocs.io/en/latest/

  • 其他PyAutoGUI等。。。

python 模拟键盘鼠标输入

PyAutoGUI-python版的autoit/AHK

 

2.下载地址

http://www.ghost-mouse.com/

https://en.uptodown.com/windows/automatization  >>  https://tinytask.en.uptodown.com/windows

https://www.autoitscript.com/site/autoit/downloads/

3.工具简介

(1)GhostMouse导出文件可读,可以通过脚本提取鼠标轨迹和键盘输入内容。但是通过命令行运行所导出的文件,只是自动打开程序,还需要通过快捷键运行回放。

(2)tinytask导出文件不可读,可以导出编译好的exe,通过命令行可以直接无界面回放。

Python模块学习:subprocess 创建子进程

Python执行系统命令,os.system && os.popen && subprocess.Popen

4.python调用外部脚本或程序(如tinytask)

import subprocess
def run_tinytask(rec_file_compiled):
    # 运行外部脚本,传入参数和接受print内容
    # p = subprocess.Popen(['python','xxx.py','-n',sth_to_pass],stdout=subprocess.PIPE)
    # result = p.stdout.read()
    
    # 参考pytesseract.py
    command = [rec_file_compiled]
    # proc = subprocess.Popen(command, stderr=subprocess.PIPE)
    proc = subprocess.Popen(command)
    status = proc.wait()
    # error_string = proc.stderr.read()
    # proc.stderr.close()
    # print status, error_string
    return status

def main():
    rec_file_compiled = 'G:/pydata/install/test.exe'  # tinytask导出的编译文件
    print run_tinytask(rec_file_compiled)
    print 'finished'
    
if __name__ == '__main__':
    main()

5.通过脚本提取GhostMouse记录的鼠标轨迹和键盘输入内容

(1)参考

滑动验证码破解:Python Selenium 2.0 应用

(2)代码实现

#coding:utf-8


# GhostMouse导出的rms文件
# {Delay 2.13}
# {Move (1225,349)}
# {Delay 0.23}

# {Move (729,657)}
# {Delay 1.65}
# {LMouse down (727,658)}  #鼠标按下开始拖动
# {Delay 0.99}
# {Move (727,658)}
# {Delay 0.11}

# {Move (790,659)}
# {Delay 0.91}
# {LMouse up (790,659)}     #鼠标释放结束拖动


import os

# xyd_typical = (1,0,0.04)
def read_rms(file_rms):

    with open(file_rms) as fp:  #os.path.sep
        LMouse_down = False

        for line in fp:
            #{LMouse down (727,658)}
            # {Delay 1.65}
            if 'LMouse down' in line or (LMouse_down == True and 'Move' in line):
                if 'LMouse down' in line:
                    LMouse_down = True
                    xyd_records = []
                    x_last, y_last = 0, 0  #保证第一个偏移量为实际开始位置
                xy_pos = line.split('(')[1].split(')')[0].split(',')
                x_pos, y_pos = [int(i) for i in xy_pos]
                continue
            # {Move (729,657)}
            # {Delay 1.65}            
            if LMouse_down == True and 'Delay' in line: 
                x_delta, y_delta = x_pos-x_last,  y_pos-y_last
                if x_delta == 0 and y_delta == 0 and len(xyd_records) != 0:  #len 可能起点就是0,0
                    continue
                else:
                    delay = float(line.split(' ')[1].split('}')[0])
                    xyd_records.append((x_delta, y_delta, delay))    
                    x_last, y_last = x_pos, y_pos
                    continue
                
            # {LMouse up (790,659)}    
            if LMouse_down == True and 'LMouse up' in line:    
                # x_init y_init x_change y_change
                # x y d 每一次偏移量
                # x y d            
                with open(file_txt,'a') as fh_txt:
                    # x_change = sum(x for x,y,d in xyd_records)
                    x_init = xyd_records[0][0]
                    y_init = xyd_records[0][1]
                    x_change = sum(x for x,y,d in xyd_records[1:])
                    y_change = sum(y for x,y,d in xyd_records[1:])
                    fh_txt.write('{} {} {} {}\n'.format(x_init, y_init, x_change, y_change))  #加os.linesep是'\r\n'
                    for x,y,d in xyd_records[1:]:  #第一个记录为起始位置,value记录之后的每一次偏移
                        fh_txt.write('{} {} {}\n'.format(x, y, d))
                LMouse_down = False
                xyd_records = []                    
                

def read_txt(file_txt):
    with open(file_txt,'r') as fp:
        result = {}  #(x_init, y_init, x_change, y_chang): [(x0,y0,d0), (x1,y1,d1)...]
        
        for line in fp:
            line = line.strip().split()
            if len(line) == 4:
                key = tuple([int(i) for i in line])
                result[key] = []
            elif len(line) == 3:
                x,y,d = line
                x,y,d = int(x), int(y), float(d)
                result[key].append((int(x), int(y), float(d)))        
    return result
                

if __name__ == '__main__':
    
    file_rms = os.path.join(os.path.abspath('.'),'mouse.rms')
    file_txt = os.path.join(os.path.abspath('.'),'mouse.txt')
    read_rms(file_rms)
    result = read_txt(file_txt)
    for k,v in result.items():
        print k,v

        

 

6.selenium+autopy实现右键另存为

#!/usr/bin/env python
# -*- coding: UTF-8 -*
import time
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
# import autopy
from autopy import key, mouse


driver = webdriver.Chrome()
# driver = webdriver.Firefox()

driver.get('http://www.baidu.com')
# <a class="mnav" href="http://news.baidu.com" name="tj_trnews">新闻</a>
e = driver.find_element_by_partial_link_text(u'新闻')   #页面显示的链接文字,而不是具体链接地址,所以'news'不行!!!
# e = driver.find_element_by_name('tj_trnews')

# fifefox geckodriver context_click异常
# https://stackoverflow.com/questions/6927229/context-click-in-selenium-2-2
# http://bbs.csdn.net/topics/392058306
# https://stackoverflow.com/questions/40360223/webdriverexception-moveto-did-not-match-a-known-command

ActionChains(driver).context_click(e).perform()  
# ActionChains(driver).move_to_element(e).context_click(e).perform() #也行
# mouse.click(mouse.RIGHT_BUTTON)
time.sleep(1.5)
key.type_string('k')
time.sleep(1.5)
key.type_string(time.strftime('%H%M%S'))
key.tap(key.K_RETURN)

 

posted @ 2017-07-24 21:48  my8100  阅读(4980)  评论(0编辑  收藏  举报