0.关键实现:程序窗口前置
1.参考
autopy (实践见最后一章节)
autopy.mouse.smooth_move(1, 1) 可以实现平滑移动
pip install PyUserInput
autoit
win32api
pyautowin
其他PyAutoGUI等。。。
2.下载地址
3.工具简介
(1)GhostMouse导出文件可读,可以通过脚本提取鼠标轨迹和键盘输入内容。但是通过命令行运行所导出的文件,只是自动打开程序,还需要通过快捷键运行回放。
(2)tinytask导出文件不可读,可以导出编译好的exe,通过命令行可以直接无界面回放。
4.python调用外部脚本或程序(如tinytask)
importsubprocessdefrun_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
returnstatusdefmain():
rec_file_compiled= 'G:/pydata/install/test.exe' #tinytask导出的编译文件
printrun_tinytask(rec_file_compiled)print 'finished'
if __name__ == '__main__':
main()
5.通过脚本提取GhostMouse记录的鼠标轨迹和键盘输入内容
(1)参考
(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)} #鼠标释放结束拖动
importos#xyd_typical = (1,0,0.04)
defread_rms(file_rms):
with open(file_rms) as fp:#os.path.sep
LMouse_down =Falsefor line infp:#{LMouse down (727,658)}
#{Delay 1.65}
if 'LMouse down' in line or (LMouse_down == True and 'Move' inline):if 'LMouse down' inline:
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 inxy_pos]continue
#{Move (729,657)}
#{Delay 1.65}
if LMouse_down == True and 'Delay' inline:
x_delta, y_delta= x_pos-x_last, y_pos-y_lastif x_delta == 0 and y_delta ==0 and len(xyd_records) != 0: #len 可能起点就是0,0continue
else:
delay= float(line.split(' ')[1].split('}')[0])
xyd_records.append((x_delta, y_delta, delay))
x_last, y_last=x_pos, y_poscontinue
#{LMouse up (790,659)}
if LMouse_down == True and 'LMouse up' inline:#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=[]defread_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 infp:
line=line.strip().split()if len(line) == 4:
key= tuple([int(i) for i inline])
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)))returnresultif __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 inresult.items():printk,v
6.selenium+autopy实现右键另存为
#!/usr/bin/env python#-*- coding: UTF-8 -*
importtimefrom selenium importwebdriverfrom selenium.webdriver.common.action_chains importActionChains#import autopy
from autopy importkey, mouse
driver=webdriver.Chrome()#driver = webdriver.Firefox()
driver.get('http://www.baidu.com')#新闻
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)