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 ...
随机推荐
- java从Swagger Api接口获取数据工具类
- Debugging Beyond Visual Studio – WinDbg
Getting started with WinDbg: 1. Download the Debugging Tools for Windows from the Microsoft website ...
- git 原理
1.git基本原理 2.git提交代码到远程仓库 3.远程仓库同步到本地 git pull #等同于下面命令 git fetch git merge 3.提交代码是冲突解决 一般提交前先get pul ...
- windows下零基础gulp构建
在学习前,先谈谈大致使用gulp的步骤,给读者以初步的认识.首先当然是安装nodejs,通过nodejs的npm全局安装和项目安装gulp,其次在项目里安装所需要的gulp插件,然后新建gulp的配置 ...
- 【LOJ6067】【2017 山东一轮集训 Day3】第三题 FFT
[LOJ6067][2017 山东一轮集训 Day3]第三题 FFT 题目大意 给你 \(n,b,c,d,e,a_0,a_1,\ldots,a_{n-1}\),定义 \[ \begin{align} ...
- 洛谷P1072 Hankson 的趣味题(题解)
https://www.luogu.org/problemnew/show/P1072(题目传送) 数学的推理在编程的体现越来越明显了.(本人嘀咕) 首先,我们知道这两个等式: (a0,x)=a1,[ ...
- 一.Django 学习 —— 环境搭建
Ⅰ.前言 Django是一个开放源代码的Web应用框架,由Python写成.采用了MVC的框架模式,即模型M,视图V和控制器C. 我们先搭建一个Django项目运行的环境. 需要准备的有: 1- Py ...
- JQGrid导出Excel文件
系列索引 Web jquery表格组件 JQGrid 的使用 - 从入门到精通 开篇及索引 Web jquery表格组件 JQGrid 的使用 - 4.JQGrid参数.ColModel API.事件 ...
- 第四届CCCC团体程序设计天梯赛 后记
一不小心又翻车了,第二次痛失200分 1.开局7分钟A了L2-3,一看榜已经有七个大兄弟排在前面了,翻车 * 1 2.把L1-3 A了18分,留了两分准备抢顽强拼搏奖,最后五秒钟把题过了,万万没想到还 ...
- nodemon 热更新
sudo npm i -g nodemon nodemon app.js