在pywinauto中使用wait超时,但睡眠工作正常



我正在尝试在Windows中自动化一些GUI内容。我打开了一个设置窗口,但在继续之前等待它打开时,它超时了:

from pywinauto import Application
app = Application()
app.start(r"explorer shell:::{05d7b0f4-2121-4eff-bf6b-ed3f69b894d9}")
app.window(title_re=".*Notification Area Icons*").wait("exists", timeout=20)
app = Application(backend="uia").connect(title_re=".*Notification Area Icons*")
main_dlg = app.window(title_re=".*Notification Area Icons*")
main_dlg.print_control_identifiers()

即使超时为20秒,它也会超时。但是,如果我用sleep(5)替换app.window(title_re=".*Notification Area Icons*").wait("exists", timeout=20)(并添加适当的导入(,它就可以正常工作。我知道窗口标题是正确的,因为当使用sleep时,它会打印出控件ID。我试过用";存在";以及";可见";等待也是如此。我认为其他选择更严格,所以我不确定为什么等待不起作用。

explorer.exe进程可能很少。您可以尝试与流程无关的方式:

from pywinauto import Desktop
window_wrapper = Desktop(backend="uia").window(title_re=".*Notification Area Icons*").wait("exists", timeout=20)
print(window_wrapper.process_id())
# and compare with print(app.process)

最新更新