DEV Community

Raghwendra Sonu
Raghwendra Sonu

Posted on

Windows Desktop Automation with pyWinAuto

pywinauto is a set of python modules to automate the Microsoft Windows GUI. At its simplest it allows you to send mouse and keyboard actions to windows dialogs and controls.

Installation- > pip install pywinauto

To use this tool, you need to get attributes of your application. For this purpose you can use PyInspect tool. Clone the below repository:
https://github.com/dm-vodopyanov/py_inspect

and run python py_inspect.py from command prompt.

Alt Text

You can inspect the properties of all the elements using this tool.

Later, you need to use these attributes for automation using pyWinAuto.

I will write a python file that will do following stuffs:

  1. launch Notepad
  2. type 99999 in the notepad
  3. Click on Edit -> replace
  4. Click on Cancel button on the Replace Dialog box.
  5. Again it will enter "Hi from Python...." text in the notepad
  6. It will navigate to File-> Save menu
  7. Save the file with name "Test"

Here is the pyWinAuto code for this use case:

from pywinauto import application
app=application.Application(backend="uia")
try:
app.start("Notepad.exe")
app.window().wait('visible')
app.window(best_match='Untitled - Notepad').type_keys('99999')
app.UntitledNotepad.menu_select("Edit -> Replace")
app.Dialog.CancelButton.click()
app.UntitledNotepad.Edit.type_keys("Hi from Python.....")
app.Dialog.menu_select("File->Save")
app.Dialog.Pane.ComboBox0.Edit5.type_keys("Test")
app.Dialog.Save.click()
app.window(title='Test - Notepad').wait('ready', timeout=10)
except TimeoutError as e:
print(e)
raise e

Alt Text

Here is the execution video.
https://vimeo.com/389630552

Hope this was helpful. For more details go through pyWinAuto documentation: https://pywinauto.readthedocs.io/en/latest/

Top comments (0)