Pywinauto 0.6.4执行操作耗时约10秒,求优化建议及控件标识符输出
Hey there! Let's tackle this slow Pywinauto issue you're facing with version 0.6.4—10 seconds per operation is definitely way too slow, especially since everything's working functionally. Since you're new to the library, there are a few common pitfalls that might be causing this, plus I'll cover how to get those control identifiers you need. Let's dive in:
Pick the right backend for your app
Pywinauto relies on two core backends:win32anduia. The default might not be the fastest fit for your application. Older Win32 apps work best with thewin32backend (it's lighter and faster), while modern UWP/WPF apps requireuia. Always specify the backend explicitly when launching or connecting to your app:from pywinauto import Application # For Win32 apps app = Application(backend="win32").start("your_app.exe") # For UWP/WPF apps app = Application(backend="uia").connect(title="Your Running App")Use precise control selectors
Vague selectors likeapp.window().Button()force Pywinauto to scan every control in the window, which eats up time. Be as specific as possible by using properties likename,control_id, orclass_name:# Slow: Scans all buttons to find a match app.window().Button().click() # Fast: Targets the exact button immediately app.window(title="Main Window").Button(name="Submit", control_id=1005).click()Skip unnecessary idle waits
Some methods default to waiting for the app to be "idle" after an action, which can add significant delay. If your app doesn't need this (e.g., it responds quickly to clicks), disable it:app.window().Button(name="OK").click(wait_for_idle=False)Just make sure your app can handle back-to-back actions without being idle first—test this carefully to avoid errors.
Consider upgrading Pywinauto
Version 0.6.4 is a bit outdated, and newer releases include performance tweaks and bug fixes. If you can, upgrade to the latest stable version (just be sure to test your script afterward to avoid breaking changes):pip install --upgrade pywinauto
Control identifiers (like control IDs, names, automation IDs) are key to writing fast, reliable scripts. Here are the best ways to get them:
Use
print_control_identifiers()
Once you've connected to your app, call this method on a window object to dump all controls and their properties:app = Application(backend="win32").connect(title="Your App Window") app.window(title="Your App Window").print_control_identifiers()You'll get output like this, which lists every control's details:
Control Identifiers:
Button - 'Save' (L200, T150, R280, B175)
control_id: 1003
class_name: 'Button'
Edit - 'Username' (L50, T80, R350, B105)
control_id: 1001
class_name: 'Edit'Use Windows' built-in Inspect.exe
This native tool is perfect for digging into control properties. You can find it atC:\Windows\System32\inspect.exe(or just search for "Inspect" in the Start Menu). Hover over any control in your app, and it will show you all relevant identifiers—great for cases whereprint_control_identifiers()misses something.Use Swapy (included with Pywinauto 0.6.4)
Swapy is a GUI tool that comes bundled with older Pywinauto versions. Run it by typingswapyin your command prompt, then connect to your app. It displays a tree of all controls and their properties, and even lets you generate Pywinauto code snippets directly—super helpful for beginners.
Give these tips a try—starting with the correct backend and precise selectors should cut down that 10-second delay drastically. And using those control identifiers will make your scripts both faster and more resilient to app changes!
内容的提问来源于stack exchange,提问作者Satish Michael

