首页 > 解决方案 > pywinauto 不将控件标识为 TabControl

问题描述

Inspect.exe已将控件标识为 TabControl,但 pywinauto 无法识别。图片如下**
Inspect.exe 识别 TabControl


dump_tree给出以下,控件类型是"C1.Win.C1Command.C1DockingTabPage"哪个是组件一个tabcontrol

Control Identifiers:

WindowsForms10.Window.8.app.0.13965fa_r6_ad1 - ''    (L42, T31, R1334, B694)
['Alloy Configuration TypeWindowsForms10.Window.8.app.0.13965fa_r6_ad11', 'Alloy Configuration TypeWindowsForms10.Window.8.app.0.13965fa_r6_ad1', 'Alloy Configuration TypeWindowsForms10.Window.8.app.0.13965fa_r6_ad10', 'WindowsForms10.Window.8.app.0.13965fa_r6_ad10', 'WindowsForms10.Window.8.app.0.13965fa_r6_ad1', 'WindowsForms10.Window.8.app.0.13965fa_r6_ad11']
child_window(auto_id="AlloyTabDocument", control_type="C1.Win.C1Command.C1DockingTab")
   | 
   | WindowsForms10.Window.8.app.0.13965fa_r6_ad1 - 'Alloy Configuration'    (L240, T32, R1333, B693)
   | ['Alloy Configuration', 'Alloy ConfigurationWindowsForms10.Window.8.app.0.13965fa_r6_ad1', 'WindowsForms10.Window.8.app.0.13965fa_r6_ad12']
   | child_window(title="Alloy Configuration", auto_id="tabAlloyConfiguration", control_type="C1.Win.C1Command.C1DockingTabPage")

pywinauto 的HwndWrapper对象不是 tabcontrol:

>>> AlloyTabDocument  = addConfigWnd.child_window(auto_id="AlloyTabDocument")
>>> print(AlloyTabDocument.wrapper_object())
hwndwrapper.HwndWrapper - '', WindowsForms10.Window.8.app.0.13965fa_r6_ad1

标签: pythonpywinauto

解决方案


此问题的根本原因是 MS UI 自动化方法SelectionItemPattern.Select无任何异常退出,但实际上对给定小部件没有任何作用。

解决方案是通过ILegacyIAccessibleProvider::DoDefaultAction方法选择选项卡。
此代码适用于 pywinauto 0.6.8:

import pywinauto
from pywinauto.application import Application
import pywinauto.uia_defines as uia_defs

app = Application(backend="uia").connect(path='wfControlExplorer.exe')
explorer = app.window(class_name="WindowsForms10.Window.8.app.0.1a8c1fa_r14_ad1", auto_id='Explorer')
overview = explorer.child_window(class_name='WindowsForms10.Window.8.app.0.1a8c1fa_r14_ad1', auto_id='Overview')
tabs = overview.child_window(class_name="WindowsForms10.Window.8.app.0.1a8c1fa_r14_ad1", control_type="Tab")
# tabs.dump_tree()
# tabs.select(2)
uia_defs.get_elem_interface(tabs.children()[2].element_info.element, "LegacyIAccessible").DoDefaultAction()

推荐阅读