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/

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

Top comments (0)

The Most Contextual AI Development Assistant

Pieces.app image

Our centralized storage agent works on-device, unifying various developer tools to proactively capture and enrich useful materials, streamline collaboration, and solve complex problems through a contextual understanding of your unique workflow.

👥 Ideal for solo developers, teams, and cross-company projects

Learn more

AWS GenAI LIVE!

GenAI LIVE! is a dynamic live-streamed show exploring how AWS and our partners are helping organizations unlock real value with generative AI.

Tune in to the full event

DEV is partnering to bring live events to the community. Join us or dismiss this billboard if you're not interested. ❤️