【问题标题】:Select an item from a combobox using pywinauto使用 pywinauto 从组合框中选择一个项目
【发布时间】:2021-07-04 02:55:44
【问题描述】:

我正在尝试使用 pywinauto 自动化 Visual Basic 应用程序,后端为“win32”。除了从其中一个组合框中选择一个项目之外,我能够处理所有事情。这个特定的组合框取决于另一个组合框的选择

代码如下:

app.ThunderRT6MDIForm.xxxxx.ComboBox3.select("abc") # this correctly selects from the combobox
app.ThunderRT6MDIForm.xxxxx.ComboBox4.select(1) #This one gives error

同样的错误:

IndexError: Combobox has 0 items, you requested item 1 (0 based)

控制标识符返回:

        ComboBox - 'abc'    (L136, T206, R376, B227)
       | ['ComboBox3', 'abc co-brandingComboBox2']
       | child_window(title="abc", class_name="ThunderRT6ComboBox")
       | 
       |    | Edit - ''    (L139, T234, R356, B249)
       |    | ['abc co-brandingEdit10', 'Edit12']
       |    | child_window(class_name="Edit")

        ComboBox - ''    (L136, T157, R376, B178)
       | ['4', 'ComboBox4']
       | child_window(class_name="ThunderRT6ComboBox")
       |    | 
       |    | Edit - ''    (L139, T160, R356, B175)
       |    | ['5', 'Edit14']
       |    | child_window(class_name="Edit")

【问题讨论】:

  • 这似乎是基于您尝试按索引选择一个项目的错误,但组合框中有 0 个项目,因此索引 1 处没有任何内容。您可能必须先触发该事件填充第二个组合框
  • @majita 第一个组合框 (Comboboxx3) 在尝试第二个组合框 (Combobox4) 之前已正确填充
  • 在这些操作之间添加time.sleep(2)后是否有效?
  • @VasilyRyabov 不,即使添加睡眠时间也不起作用
  • @VasilyRyabov 我现在已经在问题中添加了控制标识符

标签: pywinauto


【解决方案1】:

我找到了解决此问题的临时解决方法。我发现按 Alt + Down 键打开了组合框并给了我列表。所以,我在代码中也使用了相同的逻辑,并且成功了!

 app.ThunderRT6MDIForm.xxxxx.ComboBox4.Edit14.type_keys("%{DOWN}")
 app.ThunderRT6MDIForm.xxxxx.ComboBox4.select("item") 

【讨论】:

  • 好!虽然我不认为这是暂时的。 :) 因为这个组合框可能具有在下拉操作时执行的延迟初始化。所以这可能是唯一的方法,它是一种解决方案,而不是一种解决方法。 :)
  • 顺便说一下,UIA 后端可以使用相同的操作执行.expand(),而无需直接输入。但不确定 UIA API 是否正确检测到此 ExpandCollapsePattern。需要尝试。
【解决方案2】:

试试这个方法:(不要忘记传递 combo_box 元素)

def set_combo_box_item_that_starts_with(combo_box, searched_string):
    for text in combo_box.GetProperties()['texts']:
        if text.startswith(searched_string):
            combo_box.select(searched_string)
    return None

【讨论】:

    【解决方案3】:

    又一个变种:

    loginWindow["Edit1"].type_keys("%{DOWN}")
    loginWindow.child_window(title="choiceYouWant", control_type="ListItem").click_input()
    

    【讨论】:

      【解决方案4】:

      以前的答案对我不起作用。

      这是我的实现方式。

      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
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-11-28
        • 2013-12-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多