python模拟鼠标键盘操作 GhostMouse tinytask 调用外部脚本或程序 autopy右键另存为
0.关键实现:程序窗口前置
python 通过js控制滚动条拉取全文 通过psutil获取pid窗口句柄,通过win32gui使程序窗口前置 通过pyauto实现右键菜单和另存为操作
1.参考
- autopy (实践见最后一章节)
AutoPy Introduction and Tutorial
autopy.mouse.smooth_move(1, 1) 可以实现平滑移动
- pip install PyUserInput
[python3.5][PyUserInput]模拟鼠标和键盘模拟
- autoit
selenium webdriver 右键另存为下载文件(结合robot and autoIt)
- win32api
- pyautowin
https://pyautogui.readthedocs.io/en/latest/
- 其他PyAutoGUI等。。。
2.下载地址
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执行系统命令,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)
python模拟鼠标键盘操作 GhostMouse tinytask 调用外部脚本或程序 autopy右键另存为的更多相关文章
- python模拟鼠标拖动操作的方法
本文实例讲述了python模拟鼠标拖动操作的方法.分享给大家供大家参考.具体如下: pdf中的书签只有页码,准备把现有书签拖到一个目录中,然后添加自己页签.重复的拖动工作实在无趣,还是让程序帮我实现吧 ...
- 将CodedUI Test 放到控制台程序中,模拟鼠标键盘操作
CodedUI Test是微软的自动化测试工具,在VS中非常好用.可以用来模拟鼠标点击,键盘输入.但执行的时候必须要用mstest调用,无法传入参数(当然可以写入config文件中,但每次修改十分麻烦 ...
- selenium webdriver从安装到使用(python语言),显示等待和隐性等待用法,切换窗口或者frame,弹框处理,下拉菜单处理,模拟鼠标键盘操作等
selenium的用法 selenium2.0主要包含selenium IDE 和selenium webDriver,IDE有点类似QTP和LoadRunner的录制功能,就是firefox浏览器的 ...
- python selenium鼠标键盘操作(ActionChains)
用selenium做自动化,有时候会遇到需要模拟鼠标操作才能进行的情况,比如单击.双击.点击鼠标右键.拖拽等等.而selenium给我们提供了一个类来处理这类事件--ActionChains sele ...
- selenium模拟鼠标键盘操作
简单操作: 1.点击(鼠标左键)页面按钮:click() 2.清空输入框:clear() 3.输入字符串:send_keys()submit提交表单: 1.一般情况可以点击搜索按钮来搜索 2.也可以用 ...
- selenium webdriver(4)---模拟鼠标键盘操作
webdriver提供Actions来模拟鼠标悬浮.拖拽和键盘输入等操作,详细代码见org.openqa.selenium.interactions.Actions.本文通过几个实例来说明Action ...
- Java+selenium之WebDriver模拟鼠标键盘操作(六)
org.openqa.selenium.interactions.Actions类,主要定义了一些模拟用户的鼠标mouse,键盘keyboard操作.对于这些操作,使用 perform()方法进行执行 ...
- selenium webdriver模拟鼠标键盘操作
在测试使用Selenium webdriver测试WEB系统的时候,用到了模拟鼠标.键盘的一些输入操作. 1.鼠标的左键点击.双击.拖拽.右键点击等: 2.键盘的回车.回退.空格.ctrl.alt.s ...
- selenuim2模拟鼠标键盘操作
有时候有些元素不便点击或者做其他的操作,这个时候可以借助selenium提供的Actions类,它可以模拟鼠标和键盘的一些操作,比如点击鼠标右键,左键,移动鼠标等操作.对于这些操作,使用perform ...
随机推荐
- Announcing Microsoft Research Open Data – Datasets by Microsoft Research now available in the cloud
The Microsoft Research Outreach team has worked extensively with the external research community to ...
- 四、Java多人博客系统-2.0版本
由于时间关系,多人博客系统这里穿插一个2.0版本. 2.0版本本来是打算用于建立个人网站,但是后来发现个人建站需要购买域名服务器,还需要备案,很繁琐.最终放弃.完成此版本,最终也只是作为技术演练.此版 ...
- Kubernetes从懵圈到熟练:读懂这一篇,集群节点不下线
排查完全陌生的问题,完全不熟悉的系统组件,是售后工程师的一大工作乐趣,当然也是挑战.今天借这篇文章,跟大家分析一例这样的问题.排查过程中,需要理解一些自己完全陌生的组件,比如systemd和dbus. ...
- mysql慢查询日志按天切割归纳
问题描述: mysql开启慢查询功能,再正常不过,那么存在这样一种情况:慢查询写入的文件位置和文件名是指定好的,如果慢查询时间设定严苛,不出意外,记录慢查询的单个文件大小会日益增大,几十兆或者上百兆, ...
- Swagger - ui 学习
今天同组的打伙伴给介绍了 Swagger-ui 这个 自动生成 接口文档的 工具,感觉比较方便好用, 遂决定 学习一下, 开个随笔进行随时记录,同时也是提醒自己 先保存两篇感觉还不错的文章 : htt ...
- Vue(小案例_vue+axios仿手机app)_图片列表操作
一.前言 1.让图片还没有被完全加载出来的时候给用户提示 2.图片查看器 二.主要内容 1.让图片还没有被完全加载出来的时候 ...
- IMPLEMENTING A GRU/LSTM RNN WITH PYTHON AND THEANO - 学习笔记
catalogue . 引言 . LSTM NETWORKS . LSTM 的变体 . GRUs (Gated Recurrent Units) . IMPLEMENTATION GRUs 0. 引言 ...
- 如何巧妙的使用ArrayList的Clone方法
一.ArrayList的Clone方法的源码 返回一个Object对象,所以在使用此方法的时候要强制转换. ArrayList的本质是维护了一个Object的数组,所以克隆也是通过数组的复制实现的,属 ...
- Beamer 目录分栏
导言区加入 \usepackage{multicol} 然后 \section*{目录} \frame{\begin{multicols}{} \tableofcontents[hideallsubs ...
- MD5 两次加密
1.添加依赖 <dependency> <groupId>commons-codec</groupId> <artifactId>commons-cod ...