|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
执行下面命名,获取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[0]))
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
这个问题的原因是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窗口控件了。
|
|