DEV Community

Raghwendra Sonu
Raghwendra Sonu

Posted on

9 1

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/

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

Top comments (0)

nextjs tutorial video

Youtube Tutorial Series 📺

So you built a Next.js app, but you need a clear view of the entire operation flow to be able to identify performance bottlenecks before you launch. But how do you get started? Get the essentials on tracing for Next.js from @nikolovlazar in this video series 👀

Watch the Youtube series