在pywinauto中处理自定义ComboBox

问题描述

我正在编写一个脚本来选择ComboBox中的区域。我可以使用app.dialog['Region:ComboBox'].select(index),但不能使用app.dialog['Region:ComboBox'].select('string')。我注意到ComboBox是自定义的,并且是实时生成的。如何使用字符串选择适当的选项?该字符串将是美国西部,美国东部等地区。

   |    | GroupBox - 'Preferences'    (L811,T456,R1108,B593)
   |    | ['PreferencesGroupBox','Preferences','GroupBox','GroupBox0','GroupBox1']
   |    | child_window(title="Preferences",control_type="Group")
   |    |    | 
   |    |    | Static - 'PREFERENCES'    (L817,T462,R1102,B476)
   |    |    | ['PREFERENCES','PREFERENCESStatic','Static25']
   |    |    | child_window(title="PREFERENCES",control_type="Text")
   |    |    | 
   |    |    | Static - 'Region Text:'    (L0,T0,R0,B0)
   |    |    | ['Region Text:Static','Region Text:','Static26']
   |    |    | child_window(title="Region Text:",control_type="Text")
   |    |    | 
   |    |    | Static - ''    (L0,B0)
   |    |    | ['Static27']
   |    |    | 
   |    |    | Static - 'Region:'    (L822,T498,R937,B512)
   |    |    | ['Region:Static','Region:','Static28']
   |    |    | child_window(title="Region:",control_type="Text")
   |    |    | 
   |    |    | Custom - ''    (L947,T492,R1097,B518)
   |    |    | ['Custom3','Region:Custom']
   |    |    |    | 
   |    |    |    | ComboBox - ''    (L947,B518)
   |    |    |    | ['ComboBox','Region:ComboBox','ComboBoxESRI.ArcGIS.Azure.IaaS.Interfaces.RegionInfo']
   |    |    |    |    | 
   |    |    |    |    | Edit - ''    (L0,B0)
   |    |    |    |    | ['Edit','Edit0','Edit1']
   |    |    |    |    | child_window(auto_id="PART_EditableTextBox",control_type="Edit")
   |    |    | 
   |    |    | Static - 'Remote Desktop Port:'    (L822,T534,B548)
   |    |    | ['Remote Desktop Port:','Remote Desktop Port:Static','Static29']
   |    |    | child_window(title="Remote Desktop Port:",control_type="Text")
   |    |    | 
   |    |    | Edit - '3389'    (L947,T528,B554)
   |    |    | ['Edit2','Remote Desktop Port:Edit']
   |    |    | child_window(title="3389",control_type="Edit")
   |    |    |    | 
   |    |    |    | ScrollBar - ''    (L0,B0)
   |    |    |    | ['ScrollBar','ScrollBar0','ScrollBar1']
   |    |    |    | child_window(auto_id="VerticalScrollBar",control_type="ScrollBar")
   |    |    |    | 
   |    |    |    | ScrollBar - ''    (L0,B0)
   |    |    |    | ['ScrollBar2']
   |    |    |    | child_window(auto_id="HorizontalScrollBar",control_type="ScrollBar")
   |    |    |    | 
   |    |    |    | Button - 'r'    (L0,B0)
   |    |    |    | ['r','Button8','rButton']
   |    |    |    | child_window(title="r",auto_id="PART_ClearText",control_type="Button")
   |    |    | 
   |    |    | CheckBox - 'Track application usage anonymously'    (L822,T564,B582)
   |    |    | ['Track application usage anonymously','CheckBox','Track application usage anonymouslyCheckBox','Track application usage anonymously0','Track application usage anonymously1']
   |    |    | child_window(title="Track application usage anonymously",control_type="CheckBox")
   |    |    |    | 
   |    |    |    | Static - 'Track application usage anonymously'    (L846,T565,R1043,B581)
   |    |    |    | ['Track application usage anonymously2','Track application usage anonymouslyStatic','Static30']
   |    |    |    | child_window(title="Track application usage anonymously",control_type="Text")

解决方法

这是我如何实施它。此函数更有效地模拟 select() 函数。

def comboselect(combo,sel):
    combo.type_keys("{ENTER}")          # Selects the combo box
    texts = combo.texts()               #gets all texts available in combo box
    try:
        index = texts.index(str(sel))   #find index of required selection
    except ValueError:
        return False
    sel_index = combo.selected_index()  # find current index of combo
    if(index>sel_index):
        combo.type_keys("{DOWN}"*abs(index-sel_index))
    else:
        combo.type_keys("{UP}"*abs(index-sel_index))
    return True

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...