近几天来有几个需求是软件自动化,之前没有接触过。在没有接触过自动化的情况下摸索了几天,找到了两个学习成本比较低的解决方案,采用的开发语言皆为Python。
Pywinauto
需准备如下环境
1.Python 3.x
python这样一门因为机器学习而大热的低学习成本的语言就不多做介绍了。
2.Python库 pywinauto
Github上Star700+的一个用于Windows GUI自动化库,官网上虽然写着Linux版本正在开发中,不过作者似乎咸鱼了有一段时间,Github上的最近一次更新也是在几个月之前。
3.Spy++
用于查看要进行自动化操作的软件的窗口句柄,简单易用。
Github: https://github.com/pywinauto/pywinauto
文档: https://pywinauto.readthedocs.io
该解决方案只适用于调用Winapi接口的应用程序,对于使用Java编写的Jar等其他类型的GUI应用则无法操作,局限性较大。且调试略显麻烦,当然也有可能是我没有掌握正确的代码调试姿势。该方案不重点介绍,放上核心代码稍微注释一下就掠过了。
1 | from pywinauto.application import Application |
1 | # 使用start_soft获取窗体对象以后,对控件进行操作 |
更多高级用法阅读 https://pywinauto.readthedocs.io
方案二:
sikulix http://www.sikulix.com
sikulix1.1.2 最新版官方下载地址: https://launchpad.net/sikuli/sikulix/1.1.2
简单易学的、基于OpenCV的图像识别来进行自动化的脚本图形脚本语言 —— sikuli。同时支持Java与Jython(可以使用Python2.x版本)、JRuby、Javascript来编写自动化脚本。
supports as scripting languages
- Python language level 2.7 (supported by Jython)
- running RobotFramework text-scripts is supported (see docs)
- Ruby language level 1.9 and 2.0 (supported by JRuby)
- JavaScript (supported by the Java Scripting Engine)**
sikuli还支持基本的文本识别(基于Tesseract实现)。
SikuliX comes with basic text recognition (OCR) and can be used to search text in images. This feature is powered by Tesseract.
Sikulix的基本操作
1 | openApp("Cmd Command") # 划重点:不支持中文路径 |
Sikulix全局设置
详细的可设置参数列表:http://sikulix-2014.readthedocs.io/en/latest/scripting.html#controllingsikuliscriptsandtheirbehavior
1 | Settings.option = value |