如何创建 Python 脚本来自动安装软件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27901880/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to create a Python script to automate software installation?
提问by guru
I want to automate the software installation process. The scenario is as follows:
我想自动化软件安装过程。场景如下:
Run the installation file. On first screen it has two buttons next, cancel. On click of next it goes to next screen having two buttons, next, cancel and some input data is required. After details are provided, it will show finish or cancel button.
运行安装文件。在第一个屏幕上,它接下来有两个按钮,取消。单击下一步后,它会进入下一个屏幕,有两个按钮,下一步,取消和一些输入数据是必需的。提供详细信息后,它将显示完成或取消按钮。
I want to write a Python script that would automate this activity. It should identify the button and click it. It should enter the data wherever required and finish the installation. To achieve this functionality:
我想编写一个 Python 脚本来自动化这个活动。它应该识别按钮并单击它。它应该在需要的地方输入数据并完成安装。要实现此功能:
- Python API is required, if any?
- Some code samples or link of the tutorials to use the same.
- 如果需要,是否需要 Python API?
- 使用相同的一些代码示例或教程链接。
Sample image for reference:
示例图像供参考:
回答by Vasily Ryabov
As Rawing mentioned, pywinauto is good choice for Windows installer. Here is nice sample video: http://pywinauto.github.io/
For waiting next page use something like that: app.WizardPageTitle.wait('ready')
When installer finished: app.FinishPage.wait_not('visible')
For edit box input: app.WizardPage.Edit.type_keys('some input path', with_spaces=True)
For button clicks I'd recommend click_input()
as more reliable method.
If you want to install the app on many machines automatically, you can create Remote Desktop or VNC session and run local copy of the Python script inside that session. Just do not minimize RDP or VNC window to prevent GUI context loss. Losing focus is safe and you can continue your work on master machine in another window without affecting remote installation.
Example of easy install script for FastStone Image Viewer 4.6:
正如 Rawing 所说,pywinauto 是 Windows 安装程序的不错选择。这是不错的示例视频:http: //pywinauto.github.io/
对于等待下一页,请使用类似的内容:app.WizardPageTitle.wait('ready')
安装程序完成后:app.FinishPage.wait_not('visible')
对于编辑框输入:app.WizardPage.Edit.type_keys('some input path', with_spaces=True)
对于按钮点击,我建议使用click_input()
更可靠的方法。
如果您想在多台机器上自动安装该应用程序,您可以创建远程桌面或 VNC 会话并在该会话中运行 Python 脚本的本地副本。只是不要最小化 RDP 或 VNC 窗口以防止 GUI 上下文丢失。失去焦点是安全的,您可以在另一个窗口中继续在主机上工作,而不会影响远程安装。
FastStone Image Viewer 4.6 的简易安装脚本示例:
import os
from pywinauto.application import Application
fsv = Application(backend="win32").start("FSViewerSetup46.exe")
fsv.InstallDialog.NextButton.wait('ready', timeout=30).click_input()
fsv.InstallDialog.IAgreeRadioButton.wait('ready', timeout=30).click_input()
fsv.InstallDialog.Edit.Wait('ready', timeout=30).type_keys(os.getcwd() + "\FastStone Image Viewer", with_spaces=True)
fsv.InstallDialog.InstallButton.wait('ready', timeout=30).click_input()
fsv.InstallDialog.FinishButton.wait('ready', timeout=30).click_input()