How to create a Python script to automate software

2020-02-29 01:50发布

问题:

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:

  1. Python API is required, if any?
  2. Some code samples or link of the tutorials to use the same.

Sample image for reference:

回答1:

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:

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()