python 使用 pywinauto库提示查找不到untitiled-Notepad窗口的解决办法

最近因为学习pywinauto来抓取窗体控件的handle,跟着网上的文章实现了notepad启动应用程序,但是怎么找不到Untitled - notepad元素。

app = Application(backend= 'uia').start('notepad.exe') 
dlg = app['Untitled - Notepad']
time.sleep(2)
dlg.print_control_identifiers(filename =None)

运行上图的代码最后报的却是

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "D:\Python_Project_Practice\main.py", line 94, in <module>
    dlg.print_control_identifiers(filename =None)
  File "D:\Programers_installation\python\Lib\site-packages\pywinauto\application.py", line 613, in print_control_identifiers
    this_ctrl = self.__resolve_control(self.criteria)[-1]
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\Programers_installation\python\Lib\site-packages\pywinauto\application.py", line 261, in __resolve_control
    raise e.original_exception
  File "D:\Programers_installation\python\Lib\site-packages\pywinauto\timings.py", line 436, in wait_until_passes
    func_val = func(*args, **kwargs)
               ^^^^^^^^^^^^^^^^^^^^^
  File "D:\Programers_installation\python\Lib\site-packages\pywinauto\application.py", line 203, in __get_ctrl
    dialog = self.backend.generic_wrapper_class(findwindows.find_element(**criteria[0]))
                                                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\Programers_installation\python\Lib\site-packages\pywinauto\findwindows.py", line 87, in find_element
    raise ElementNotFoundError(kwargs)
pywinauto.findwindows.ElementNotFoundError: {'best_match': 'Untitled - Notepad', 'backend': 'uia', 'process': 8464}

查找了网上很多文章也有小伙伴遇到了相同的问题,但是无解决办法

我使用inspect工具定位窗体确认了title的命名是没有问题的

最后没办法了,只能科学上网去寻找解决办法,通过一番摸索,终于找到一个pywinauto教程视频,成功解决了这个问题,直接上代码

app = Application(backend= 'uia').start('notepad.exe') .connect(title = 'Untitled - Notepad', timeout =20)
dlg = app['Untitled - Notepad']
time.sleep(2)
dlg.print_control_identifiers(filename =None)

只需要在后边使用connect方法先定位到Untitled -Notepad的窗体控件

Dialog - 'Untitled - Notepad'    (L943, T174, R1857, B914)
['Untitled - Notepad', 'Dialog', 'Untitled - NotepadDialog']
child_window(title="Untitled - Notepad", control_type="Window")
   | 
   | Pane - ''    (L951, T267, R1849, B866)
   | ['UntitledPane', 'Pane', 'UntitledPane0', 'UntitledPane1', 'Pane0', 'Pane1']
   |    | 
   |    | Document - ''    (L951, T267, R1849, B866)
   |    | ['Document', 'UntitledDocument']
   | 
   | Pane - ''    (L1001, T186, R1288, B218)
   | ['UntitledPane2', 'Pane2']
   |    | 
   |    | TabControl - ''    (L1001, T186, R1360, B226)
   |    | ['TabControl', 'UntitledTabControl', 'TabControlAdd New Tab']
   |    | child_window(auto_id="Tabs", control_type="Tab")
   |    |    | 
   |    |    | ListBox - ''    (L1003, T186, R1315, B226)
   |    |    | ['UntitledListBox', 'ListBox']
   |    |    | child_window(auto_id="TabListView", control_type="List")
   |    |    |    | 
   |    |    |    | TabItem - 'Untitled. Unmodified.'    (L1006, T186, R1313, B226)
   |    |    |    | ['Untitled. Unmodified.TabItem', 'Untitled. Unmodified.', 'TabItem']
   |    |    |    | child_window(title="Untitled. Unmodified.", control_type="TabItem")
   |    |    |    |    | 
   |    |    |    |    | Static - 'Untitled'    (L1022, T196, R1078, B215)
   |    |    |    |    | ['UntitledStatic', 'Static', 'Untitled', 'Static0', 'Static1']
   |    |    |    |    | child_window(title="Untitled", control_type="Text")
   |    |    |    |    | 
   |    |    |    |    | Button - 'Close Tab'    (L1262, T191, R1302, B221)
   |    |    |    |    | ['Close TabButton', 'Button', 'Close Tab', 'Button0', 'Button1']
   |    |    |    |    | child_window(title="Close Tab", auto_id="CloseButton", control_type="Button")
   |    |    | 
   |    |    | Button - 'Add New Tab'    (L1320, T192, R1360, B222)
   |    |    | ['Button2', 'Add New Tab', 'Add New TabButton']
   |    |    | child_window(title="Add New Tab", auto_id="AddButton", control_type="Button")
   |    | 
   |    | Pane - 'Notepad automatically saves your progress. All your content will be available the next time you open Notepad.'    (L0, T0, R0, B0)
   |    | ['Pane3', 'Notepad automatically saves your progress. All your content will be available the next time you open Notepad.', 'Notepad automatically saves your progress. All your content will be available the next time you open Notepad.Pane']
   |    | child_window(title="Notepad automatically saves your progress. All your content will be available the next time you open Notepad.", auto_id="TeachingTip", control_type="Pane")
   | 
   | Pane - ''    (L951, T226, R1669, B258)
   | ['UntitledPane3', 'Pane4']
   |    | 
   |    | Menu - ''    (L951, T226, R1150, B266)
   |    | ['Menu', 'UntitledMenu', 'Menu0', 'Menu1']
   |    | child_window(auto_id="MenuBar", control_type="MenuBar")
   |    |    | 
   |    |    | MenuItem - 'File'    (L956, T226, R1008, B266)
   |    |    | ['MenuItem', 'FileMenuItem', 'File', 'MenuItem0', 'MenuItem1']
   |    |    | child_window(title="File", auto_id="File", control_type="MenuItem")
   |    |    | 
   |    |    | MenuItem - 'Edit'    (L1018, T226, R1073, B266)
   |    |    | ['EditMenuItem', 'Edit', 'MenuItem2']
   |    |    | child_window(title="Edit", auto_id="Edit", control_type="MenuItem")
   |    |    | 
   |    |    | MenuItem - 'View'    (L1083, T226, R1145, B266)
   |    |    | ['MenuItem3', 'View', 'ViewMenuItem']
   |    |    | child_window(title="View", auto_id="View", control_type="MenuItem")
   |    | 
   |    | Button - 'Settings'    (L1801, T227, R1839, B265)
   |    | ['Settings', 'Button3', 'SettingsButton']
   |    | child_window(title="Settings", auto_id="SettingsButton", control_type="Button")
   | 
   | Pane - ''    (L951, T866, R1669, B898)
   | ['Pane5', '  Ln 1, Col 1Pane']
   |    | 
   |    | Static - '  Ln 1, Col 1'    (L971, T876, R1045, B896)
   |    | ['Static2', '  Ln 1, Col 1Static', '  Ln 1, Col 1']
   |    | child_window(title="  Ln 1, Col 1", auto_id="ContentTextBlock", control_type="Text")
   |    | 
   |    | Static - '0 characters'    (L1082, T876, R1164, B896)
   |    | ['Static3', '0 charactersStatic', '0 characters']
   |    | child_window(title="0 characters", auto_id="ContentTextBlock", control_type="Text")
   |    | 
   |    | Static - ' 100%'    (L1384, T876, R1423, B896)
   |    | [' 100%Static', ' 100%', 'Static4']
   |    | child_window(title=" 100%", auto_id="ContentTextBlock", control_type="Text")
   |    | 
   |    | Static - ' Windows (CRLF)'    (L1470, T876, R1584, B896)
   |    | ['Static5', ' Windows (CRLF)', ' Windows (CRLF)Static']
   |    | child_window(title=" Windows (CRLF)", auto_id="ContentTextBlock", control_type="Text")
   |    | 
   |    | Static - ' UTF-8'    (L1680, T876, R1725, B896)
   |    | ['Static6', ' UTF-8', ' UTF-8Static']
   |    | child_window(title=" UTF-8", auto_id="ContentTextBlock", control_type="Text")
   | 
   | TitleBar - ''    (L0, T0, R0, B0)
   | ['TitleBar']
   |    | 
   |    | Menu - '系统'    (L952, T183, R980, B211)
   |    | ['Menu2', '系统', '系统Menu', '系统0', '系统1']
   |    | child_window(title="系统", auto_id="MenuBar", control_type="MenuBar")
   |    |    | 
   |    |    | MenuItem - '系统'    (L952, T183, R980, B211)
   |    |    | ['系统MenuItem', '系统2', 'MenuItem4']
   |    |    | child_window(title="系统", control_type="MenuItem")
   |    | 
   |    | Button - '最小化'    (L1671, T175, R1731, B212)
   |    | ['最小化', '最小化Button', 'Button4']
   |    | child_window(title="最小化", control_type="Button")
   |    | 
   |    | Button - '最大化'    (L1731, T175, R1790, B212)
   |    | ['最大化Button', '最大化', 'Button5']
   |    | child_window(title="最大化", control_type="Button")
   |    | 
   |    | Button - '关闭'    (L1790, T175, R1850, B212)
   |    | ['关闭', '关闭Button', 'Button6']
   |    | child_window(title="关闭", control_type="Button")

最后便可以使用print_control_identifiers(filename =None)方法打印出窗体的树结构!

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值