PyWinAuto控件标识符中使用变量的实现方法咨询
在PyWinAuto中使用变量定位控件的解决方案
你遇到的问题是Python属性访问特性导致的:直接写.STRINGVARIABLE时,PyWinAuto会尝试寻找名为STRINGVARIABLE的控件,而非变量STRINGVARIABLE存储的G5000358。这里有两种实用的解决方法:
方法1:用getattr()函数动态获取控件
这是最贴近你原有写法的方案,通过getattr()传入变量值来定位控件:
STRINGVARIABLE = 'G5000358' # 动态获取对应名称的控件 target_control = getattr(app.RMSResidentManagementSystemPatchHighestPossibleClassificationisCUI, STRINGVARIABLE) target_control.click_input(button='left', double=True)
方法2:用child_window()精准定位(更推荐)
根据你提供的控件信息,该控件有明确的title和auto_id,直接用这些属性定位比依赖控件名称更可靠(控件名称可能随界面变化):
STRINGVARIABLE = 'G5000358' # 通过title定位控件 target_control = app.RMSResidentManagementSystemPatchHighestPossibleClassificationisCUI.child_window(title=STRINGVARIABLE, control_type="Text") target_control.double_click_input() # 如果auto_id是固定值(比如CellElement_0_0对应固定位置),也可以用auto_id定位 # target_control = app.RMSResidentManagementSystemPatchHighestPossibleClassificationisCUI.child_window(auto_id="CellElement_0_0", control_type="Text")
额外优化建议
把主窗口对象存为变量,避免重复输入冗长的窗口名称:
# 简化窗口调用 main_window = app.RMSResidentManagementSystemPatchHighestPossibleClassificationisCUI main_window.set_focus() # 后续操作直接用main_window main_window.G5000358.click_input(double=True) # 或结合变量使用 STRINGVARIABLE = 'G5000358' getattr(main_window, STRINGVARIABLE).double_click_input()
内容的提问来源于stack exchange,提问作者Matthew
相关产品推荐
相关产品推荐

