Pywinauto GUI Automation
Pywinauto is a set of Python modules for automating the Microsoft Windows GUI. It enables interaction with native Windows applications, supporting both Win32 API (default) and UI Automation (UIA) backends. The current version is 0.6.9, and the library maintains an active release cadence with regular bug fixes and enhancements.
Warnings
- breaking Pywinauto version 0.6.9 is the last release compatible with Python 2.7. Future versions (e.g., 0.7.0 and beyond) will drop Python 2.7 support, requiring Python 3.
- gotcha Pywinauto offers two backends: 'win32' (default) and 'uia'. 'win32' is suitable for older MFC/WinForms apps, while 'uia' (UI Automation) is better for modern apps (WPF, UWP, Electron). Using the wrong backend can lead to controls not being found or interactive.
- gotcha If the target application runs with elevated (administrator) privileges and your Python script does not, pywinauto may not be able to interact fully or at all due to Windows' UIPI (User Interface Privilege Isolation).
- gotcha Finding controls can be challenging. 'Magic lookup' (e.g., `app.Dialog.Button`) can be fragile. Controls may have similar names or dynamic IDs.
- gotcha For some applications, the `type_keys` method (used for keyboard input) may not work correctly if they don't handle `VK_PACKET` properly.
Install
-
pip install pywinauto
Imports
- Application
from pywinauto import Application
- Desktop
from pywinauto.Desktop import Desktop
Quickstart
from pywinauto import Application
# Start a new application
app = Application().start("notepad.exe")
# Connect to an existing application (e.g., if Notepad is already open)
# app = Application().connect(title_re="Untitled - Notepad")
# Interact with the main window
notepad_window = app.UntitledNotepad
# Select a menu item
notepad_window.menu_select("Help->About Notepad")
# Click a button on the About dialog
app.AboutNotepad.OK.click()
# Type text into the edit control
notepad_window.Edit.type_keys("Hello pywinauto!\nThis is an automated message.", with_spaces=True, with_newlines=True)
# Close the application (uncomment to run)
# notepad_window.menu_select("File->Exit")
# app.Notepad.dont_save.click() # Adjust based on actual dialog for 'Do you want to save?'