Basics
A quick walkthrough: initialize, launch or attach, find, and interact with Windows UI elements. Every step shows the Python code next to the original FlaUI C#. Pick a language tab once and the whole page follows.
Run this page
The Python snippets come from
examples/scripts/quickstart.py,
which drives the bundled test application:
1) Setup
Initialize the PythonNet bridge first. It loads the bundled FlaUI assemblies into the .NET runtime.
Required initialization
Call setup_pythonnet_bridge() before importing anything C#-backed: at the top of your
script, or at the top of conftest.py in a test suite. C# has no equivalent step, because the
assemblies are referenced at build time.
UIA2 vs UIA3
When you create the Automation object, you choose the backend:
- UIA3: modern and faster; the right choice for WPF and Store apps. This is the default to use.
- UIA2: the older managed backend. Try it for legacy WinForms apps if UIA3 struggles.
See UIA2 vs UIA3 for the details.
2) Launch or attach
Launching a new app
Launch a fresh instance when you want a clean state. In Python, the application lives on
automation.application. launch() returns nothing, so ask automation.application for the main
window afterwards.
# `with` disposes the automation and closes the application on exit.
with Automation(uia) as automation:
automation.application.launch(executable)
automation.application.wait_while_main_handle_is_missing(3000)
window = automation.application.get_main_window(automation)
cf = automation.cf
print(f"Launched: {window.title}")
Attaching to an existing app
Attach when the app is already running, for example while debugging locally.
attach_or_launch() attaches if a matching process exists and launches one otherwise.
With several copies running, attach by PID instead. See Multiple instances.
3) Inspect elements
Before you can find an element, you need to know how to identify it. Use an inspector:
- FlaUInspect: the official inspector. Shows the tree, properties, and supported patterns.
- Accessibility Insights: Microsoft's modern tool. Good for highlighting elements.
What to look for, in order of preference:
- AutomationId: a stable ID set by the developers (e.g.
TextBox). Use it whenever it exists. - Name: the visible text (e.g. "Test Checkbox"). Useful when IDs are missing, but it changes when the UI language does.
- ControlType: the kind of element (e.g.
Button). Useful to narrow a search.
4) Find elements
The ConditionFactory (automation.cf) builds typed search conditions. Pass them to the
find_* methods of any element.
text_box = window.find_first_descendant(cf.by_automation_id("TextBox")) # by AutomationId (preferred)
checkbox = window.find_first_descendant(cf.by_name("Test Checkbox")) # by Name (visible text)
buttons = window.find_all_descendants(cf.by_control_type(ControlType.Button)) # by ControlType
print(f"Found {len(buttons)} buttons")
No match: null in C#, an exception in Python
In C#, FindFirst* returns null when nothing matches. In Python it raises ElementNotFound
(from flaui.lib.exceptions), so a typo fails at the line that caused it instead of turning
into a confusing None error further down.
find_first_descendant searches the whole subtree. On very large trees, find_first_child is
faster when you know the element is a direct child.
5) Interact
Convert the generic AutomationElement to its specific type with an as_* helper. The
specific type is what gives you text, toggle(), select() and so on.
The Element Cookbook has a runnable recipe for every common control.
Programmatic vs mouse
- Programmatic (
invoke(),toggle(),text = ...) uses UIA patterns. It is fast and works while the window is in the background. Prefer it. - Mouse (
click()) moves the real cursor. It is slower and needs the element on screen. Use it when no pattern fits.
6) The post_wait shortcut
UI actions take time to land. Instead of sprinkling sleep() calls, input methods (Mouse
methods, click, TextBox.enter, Tab.select_tab_item) accept post_wait:
post_wait=True: wait until input is processed (~100 ms).post_wait=0.5: wait half a second.post_wait=callable: call your own wait function.
post_wait is a Python addition. C# FlaUI uses an explicit Wait call after the action.
7) Next steps
You've covered the basics: initialize → launch → find → interact.
- Example Gallery: complete scripts for Calculator, Notepad, and the bundled apps.
- Advanced: XPath, combined conditions, tree walking, caching, and retries.
- Utilities: waiting, keyboard and mouse, screenshots, and the other helpers ported from C#.
- Test design tips: element name maps, settings, and where to start the app.
- Troubleshooting: what to check when elements aren't found or tests are flaky.