A practical pywinauto tutorial for testers: backends, finding controls, a first script, pytest setup, CI tips, and how pywinauto compares with PyAutoGUI.
pywinauto is an open-source Python library for automating Windows desktop applications. It finds windows and controls through the operating system's accessibility layers, then clicks, types, and reads values the way a user would. For testers, that means you can write Windows UI tests in plain Python and run them with pytest.
This pywinauto tutorial walks through installation, choosing a backend, finding controls, a first working script, turning that script into a pytest test, and running it in CI. It ends with where pywinauto falls short and what to use when it does.
What is pywinauto?
pywinauto is a set of Python modules, released under the BSD license, that automate the Microsoft Windows GUI. It works at the control level rather than with screen coordinates: you ask for "the button with AutomationId LoginButton" and pywinauto locates it, even if the window moves or the screen resolution changes.
It is a good fit when:
- Your team already writes Python and wants Windows UI tests in the same stack.
- You need to automate older Win32 apps that newer tools handle poorly.
- You want a free, scriptable tool rather than a commercial IDE.
pywinauto runs on Windows only. The latest stable line is 0.6.x (0.6.9), and releases are infrequent, so check compatibility with your Python version before you commit.
How to install pywinauto
Install it with pip on the Windows machine where the app under test runs:
pip install pywinautopip pulls in its Windows dependencies, including pywin32 and comtypes. If you want screenshots, also install Pillow:
pip install pillowChoose a backend: uia or win32
pywinauto talks to applications through one of two backends. Picking the right one is the first decision in any pywinauto project.
Backend | Technology | Use it for |
|---|---|---|
| Microsoft UI Automation | WPF, UWP, WinForms, Qt, and most modern desktop apps |
| Win32 API | MFC, VB6, VCL (Delphi), classic Win32, and older WinForms apps |
The default is win32. For most modern apps, set backend="uia" explicitly. If controls are missing or show up as generic panes, try the other backend.
Find your controls
Before you write a line of automation, find out what the app exposes. Two tools help:
- Accessibility Insights for Windows or Inspect.exe (from the Windows SDK) show the UI Automation tree: control types, names, and AutomationIds.
print_control_identifiers()prints pywinauto's own view of a window and suggests the identifiers you can use.
from pywinauto import Application
app = Application(backend="uia").connect(title_re=".*Your App.*", timeout=10)
app.window(title_re=".*Your App.*").print_control_identifiers()Prefer locators in this order: AutomationId (auto_id), then name (title) plus control type (control_type), then anything else. AutomationIds survive text changes and translations. If a control has none, ask the developers to add one. It is usually a one-line change on their side and the biggest stability win available.
Your first pywinauto script
This example starts an app, logs in, and checks the welcome message. Replace the path, window title, and AutomationIds with your own from the inspection step.
from pywinauto import Application
app = Application(backend="uia").start(r"C:\Path\To\YourApp.exe")
main = app.window(title_re=".*Your App.*")
main.wait("visible", timeout=10)
main.child_window(auto_id="UsernameTextBox", control_type="Edit").set_edit_text("qa.user")
main.child_window(auto_id="LoginButton", control_type="Button").click()
welcome = main.child_window(auto_id="WelcomeLabel", control_type="Text")
welcome.wait("visible", timeout=10)
assert "qa.user" in welcome.window_text()A few things worth knowing about this script:
app.window(...)does not search immediately. It builds a specification. The search happens when you act on it, which is whywait()matters.wait()replaces sleeps. It polls until the control reaches a state such as"exists","visible","enabled", or"ready", and raises an error after the timeout. Fixedtime.sleep()calls are the most common cause of slow and flaky pywinauto suites.- Some apps hand off to a second process. If
start()returns but the window never appears underapp, the launcher spawned a different process. UseApplication(backend="uia").connect(title_re=..., timeout=10)after starting it.
Common pywinauto actions
Task | Method | Notes |
|---|---|---|
Click a button | | Uses the control's Invoke pattern, no real mouse movement |
Click like a user | | Moves the real mouse, needs an unlocked screen |
Set text | | Replaces the field's content directly |
Type keystrokes | | Sends real key presses, supports special keys |
Read text | | Returns the control's text |
Wait for a state | | Combine states with spaces |
Wait for something to go away | | Useful for progress dialogs |
Take a screenshot | | Requires Pillow |
Use click() and set_edit_text() where they work, because they do not depend on focus or mouse position. Fall back to click_input() and type_keys() for controls that only respond to real input.
Turn the script into a pytest test
Wrap app startup and teardown in a fixture so every test starts from a clean state:
import pytest
from pywinauto import Application
APP_PATH = r"C:\Path\To\YourApp.exe"
@pytest.fixture
def main_window():
app = Application(backend="uia").start(APP_PATH)
window = app.window(title_re=".*Your App.*")
window.wait("visible", timeout=10)
yield window
app.kill()
def test_login_shows_welcome_message(main_window):
main_window.child_window(auto_id="UsernameTextBox", control_type="Edit").set_edit_text("qa.user")
main_window.child_window(auto_id="LoginButton", control_type="Button").click()
welcome = main_window.child_window(auto_id="WelcomeLabel", control_type="Text")
welcome.wait("visible", timeout=10)
assert "qa.user" in welcome.window_text()As the suite grows, move locators into page-object classes, one per screen, so a changed AutomationId is a one-line fix instead of a search across every test.
Run pywinauto tests in CI
pywinauto drives a real desktop, so the CI runner needs one:
- Run as a logged-in user with an unlocked screen. UI Automation cannot reach apps on a locked or disconnected session, and
click_input()needs a visible desktop. A Windows service account will not work. - Keep the resolution fixed. It matters less for control-based locators than for image tools, but it keeps screenshots comparable.
- Save a screenshot on failure. Call
capture_as_image()from a pytest hook or anexceptblock so failed runs leave evidence. - Publish results with pytest's JUnit XML output (
pytest --junitxml=results.xml) so your CI server can show pass and fail history.
pywinauto vs PyAutoGUI
The two libraries often come up together, but they work in very different ways.
pywinauto | PyAutoGUI | |
|---|---|---|
How it finds things | Controls through UI Automation or Win32 | Screen coordinates or image matching |
Platforms | Windows only | Windows, macOS, Linux |
Survives window moves and resizes | Yes | Often no |
Reads control values | Yes | No, only pixels |
Works with the app in the background | Often, with | No, needs the screen |
Best for | Testing Windows apps | Quick cross-platform scripts and apps with no accessible controls |
For testing a Windows app, pywinauto is the stronger choice because it reads real control state you can assert on. PyAutoGUI is useful when an app exposes nothing to UI Automation, such as a game or a remote session.
pywinauto limitations
pywinauto is a capable library, but plan for these before you build a large suite on it:
- Windows only. There is no path to macOS or Linux desktop apps.
- Slow release pace. The 0.6.x line has seen infrequent updates, so you may need to work around issues yourself.
- No reporting, recorder, or test management. You assemble those from pytest plugins and your CI server.
- You own the framework. Page objects, retries, data handling, and test data cleanup are all code your team writes and maintains.
When a library is not enough
Many teams start with pywinauto and stay there happily. Others reach a point where the framework code takes more time than the tests, or where testers who do not write Python need to contribute. That is when an all-in-one tool is worth a look.
Katalon Studio is one option. From version 10.4 it includes a built-in Windows driver based on FlaUI, with a Windows Spy and Recorder, keyword and Groovy scripting in the same test, built-in reports, and CI runs through Katalon Runtime Engine (KRE). The trade-off is cost: desktop testing is part of Studio's paid plans, which start at $1,800 per seat per year, and it covers Windows desktop apps only. See Katalon desktop testing for details.
For a full comparison of free and commercial options, read our guide to desktop automation tools. If you are moving off WinAppDriver, see WinAppDriver alternatives and how to migrate.
Frequently asked questions
pywinauto automates Windows desktop applications from Python. Testers use it to click, type, and read values from controls in WPF, WinForms, UWP, and Win32 apps, usually inside pytest test suites.
pywinauto finds controls through UI Automation or the Win32 API, so it can read values and survives window moves. PyAutoGUI works with screen coordinates and images across Windows, macOS, and Linux, but cannot read control state. For testing Windows apps, pywinauto is usually the better choice.
Use backend="uia" for most modern apps, including WPF, UWP, and WinForms. Use the default win32 backend for older apps built on MFC, VB6, Delphi, or classic Win32. If controls are missing, try the other backend.
No. pywinauto runs on Windows only, because it relies on Windows accessibility and Win32 APIs.
Yes, as long as the CI agent runs as a logged-in user with an unlocked desktop. UI Automation cannot reach apps on a locked or disconnected session, so a Windows service account will not work.
The project is available and widely used, but releases are infrequent. The latest stable line is 0.6.x, so check compatibility with your Python version before you start a large suite.

Test everything. Ship with confidence.
From generating test cases to spotting failures, Katalon's AI works across your entire pipeline, automatically.




