暮雨朝云 发表于 2023-5-19 11:16:42

pywinauto

执行下面命名,获取notepad窗口控件,为什么会失败,有大佬可以告诉一下原因嘛?

from pywinauto import Application
app = Application(backend="win32").start("notepad.exe")
dlg = app["Untitled - Notepad"]
dlg.print_control_identifiers()

运行失败:
C:\Py\pythonProject\venv\Scripts\python.exe C:\Py\pythonProject\Practise\Test.py
C:\Py\pythonProject\venv\lib\site-packages\pywinauto\application.py:1076: RuntimeWarning: Application is not loaded correctly (WaitForInputIdle failed)
warnings.warn('Application is not loaded correctly (WaitForInputIdle failed)', RuntimeWarning)
Traceback (most recent call last):
File "C:\Py\pythonProject\venv\lib\site-packages\pywinauto\application.py", line 250, in __resolve_control
    ctrl = wait_until_passes(
File "C:\Py\pythonProject\venv\lib\site-packages\pywinauto\timings.py", line 458, in wait_until_passes
    raise err
pywinauto.timings.TimeoutError

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "C:\Py\pythonProject\Practise\Test.py", line 5, in <module>
    dlg.print_control_identifiers()
File "C:\Py\pythonProject\venv\lib\site-packages\pywinauto\application.py", line 613, in print_control_identifiers
    this_ctrl = self.__resolve_control(self.criteria)[-1]
File "C:\Py\pythonProject\venv\lib\site-packages\pywinauto\application.py", line 261, in __resolve_control
    raise e.original_exception
File "C:\Py\pythonProject\venv\lib\site-packages\pywinauto\timings.py", line 436, in wait_until_passes
    func_val = func(*args, **kwargs)
File "C:\Py\pythonProject\venv\lib\site-packages\pywinauto\application.py", line 203, in __get_ctrl
    dialog = self.backend.generic_wrapper_class(findwindows.find_element(**criteria))
File "C:\Py\pythonProject\venv\lib\site-packages\pywinauto\findwindows.py", line 84, in find_element
    elements = find_elements(**kwargs)
File "C:\Py\pythonProject\venv\lib\site-packages\pywinauto\findwindows.py", line 305, in find_elements
    elements = findbestmatch.find_best_control_matches(best_match, wrapped_elems)
File "C:\Py\pythonProject\venv\lib\site-packages\pywinauto\findbestmatch.py", line 536, in find_best_control_matches
    raise MatchError(items = name_control_map.keys(), tofind = search_text)
pywinauto.findbestmatch.MatchError: Could not find 'Untitled - Notepad' in 'dict_keys([])'

Process finished with exit code 1

isdkz 发表于 2023-5-19 11:17:19

这个问题的原因是pywinauto在启动应用程序时,需要等待应用程序完成启动并处于空闲状态,但是在这个例子中,启动notepad.exe时,它没有完成启动并进入空闲状态就立即尝试连接到它,因此出现了TimeoutError。要解决这个问题,可以在启动应用程序后添加一个等待时间,等待应用程序启动并进入空闲状态,然后再连接到它。例如,可以使用以下代码:

from pywinauto import Application
import time

app = Application(backend="win32").start("notepad.exe")
time.sleep(1)# 等待1秒钟
dlg = app["Untitled - Notepad"]
dlg.print_control_identifiers()

这样就可以成功获取到notepad窗口控件了。

暮雨朝云 发表于 2023-5-19 11:24:09

isdkz 发表于 2023-5-19 11:17
这个问题的原因是pywinauto在启动应用程序时,需要等待应用程序完成启动并处于空闲状态,但是在这个例子中 ...

还是不行啊,同样的运行失败{:5_107:}

暮雨朝云 发表于 2023-5-19 11:48:24

isdkz 发表于 2023-5-19 11:17
这个问题的原因是pywinauto在启动应用程序时,需要等待应用程序完成启动并处于空闲状态,但是在这个例子中 ...

同样的命令换成打开其它应用就可以,可能是notepad应用本身有啥问题,谢谢你的解答。

sfqxx 发表于 2023-5-19 17:50:55

sfqxx_小 发表于 2023-5-19 19:18:08

支持支持

平凡之路1314 发表于 2023-5-19 21:00:07

zhangjinxuan 发表于 2023-5-19 21:07:35

{:5_108:}
页: [1]
查看完整版本: pywinauto