pywinauto Tutorial: Automate Windows Apps with Python

PywinAuto tutorial

A practical pywinauto tutorial for testers: backends, finding controls, a first script, pytest setup, CI tips, and how pywinauto compares with PyAutoGUI.

Key takeaways
The 30-second version of this article
pywinauto is a free, BSD-licensed Python library for automating Windows desktop apps at the control level, not by screen coordinates.
Use the uia backend for WPF, UWP, and modern WinForms apps, and the win32 backend for MFC, VB6, Delphi, and classic Win32 apps.
Find controls with Accessibility Insights, Inspect.exe, or print_control_identifiers(), and prefer AutomationIds over names.
Replace sleeps with wait(), wrap startup in a pytest fixture, and run CI on a logged-in, unlocked Windows session.
pywinauto reads real control state you can assert on, which makes it a better fit than PyAutoGUI for testing Windows apps.

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 pywinauto

pip pulls in its Windows dependencies, including pywin32 and comtypes. If you want screenshots, also install Pillow:

pip install pillow

Choose 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

uia

Microsoft UI Automation

WPF, UWP, WinForms, Qt, and most modern desktop apps

win32

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 why wait() 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. Fixed time.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 under app, the launcher spawned a different process. Use Application(backend="uia").connect(title_re=..., timeout=10) after starting it.

Common pywinauto actions

Task

Method

Notes

Click a button

click()

Uses the control's Invoke pattern, no real mouse movement

Click like a user

click_input()

Moves the real mouse, needs an unlocked screen

Set text

set_edit_text("value")

Replaces the field's content directly

Type keystrokes

type_keys("value{ENTER}", with_spaces=True)

Sends real key presses, supports special keys

Read text

window_text()

Returns the control's text

Wait for a state

wait("visible enabled", timeout=10)

Combine states with spaces

Wait for something to go away

wait_not("visible", timeout=10)

Useful for progress dialogs

Take a screenshot

capture_as_image().save("shot.png")

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 an except block 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 click() and set_edit_text()

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

Xuan Tran
Xuan TranProduct Manager - Katalon Studio

A product builder at heart, Xuan designs for the user first — turning complex testing workflows into intuitive, AI-powered experiences. She pairs sharp product instincts with a designer's eye for the details that make software feel effortless.

Connect on LinkedIn →
Get started

Test everything. Ship with confidence.

From generating test cases to spotting failures, Katalon's AI works across your entire pipeline, automatically.

Live webinar

The Next Evolution of Manual Testing: Introducing Katalon AI Test Runner

Learn how to grow your QA team's capacity in the age of AI, then watch Katalon AI Test Runner turn manual test cases into automated runs, live.

2:00 to 3:00 PM IST
Almas AhmedSenior Account Manager
Sumit WankhedeSolutions Engineer Manager
Register now