Pywinauto: [Win32 Combobox] UIA backend cannot select by index OR value

Created on 7 Aug 2018  路  3Comments  路  Source: pywinauto/pywinauto

When working with a native Win32 ComboBox (i.e. a COMBOBOX control with CBS_DROPDOWNLIST and no other special flags), attempting to select an entry by index _or_ by value results in an error.

Test case:

from pywinauto.application import Application
app = Application(backend="uia").start('C:\\Windows\\notepad.exe')
window = app.top_window()
window.menu_select('Format -> Font')
font = window.Font
font.ScriptComboBox.select(2) # fails with NoPatternInterfaceError() thrown from uia_defs.get_elem_interface(elem, "SelectionItem")
font.ScriptComboBox.select(3) # fails with IndexError("item not found")
font.ScriptComboBox.select('Greek') # fails with IndexError("item not found")
print font.ScriptComboBox.expand().children() # returns [<uia_controls.ListViewWrapper - '[u'Western']', ListBox, ...>, <uiawrapper.UIAWrapper - 'Script:', Static, ...>, <uia_controls.ButtonWrapper - 'Close', Button, ...>]

The problem appears to be that ComboBoxWrapper.children() isn't returning the contents - instead, it's returning the ListBox (if expanded), the static Label for the combobox, and the "Open/Close" button to expand/collapse it.

Currently, the only way I can select an item is to manually .expand() the combobox, get its first child window (the ListBox), find the appropriate index (by iterating across its .texts() ), select it using .get_item(x).select(), .collapse() the combobox, and finally press Up+Down (or vice-versa) in order to ensure that CBN_SELENDOK and CBN_SELCHANGE notifications get generated.

Observed with pywinauto 0.6.5 for Python 2.7 on Windows 7, Windows 8.1, and Windows 10 build 1803.

Using the "win32" backend is not an option for me, because the application I am attempting to automate uses a WTL Ribbon control which only the UIA backend is capable of properly parsing.

bug

All 3 comments

Hi @quietust I see this combo box has child button "Open" in Inspect.exe. I think this code should work for you:

font.ScriptComboBox.OpenButton.invoke()
font.ScriptComboBox.ScriptList.select('Greek')

I can add this workaround into the code later. Please confirm it works with both Notepad and the WTL app. I haven't tested it yet. Just had a short look with Inspect.exe.

font.ScriptComboBox.ScriptList.select('Greek') doesn't work (errors with TypeError: select() takes exactly 1 argument (2 given)), but font.ScriptComboBox.ScriptList.Greek.select() (or font.ScriptComboBox.child_window(title='Greek').select()) does work (in both Notepad and the WTL app), at least on Windows 7.

However, it still isn't triggering CBN_SELENDOK / CBN_SELCHANGE notifications, which is a problem because the WTL app uses them to enable/disable other controls based on the selection; I can still use the Up+Down (or Down+Up) workaround, though I'll have to query the .selected_item_index() to decide which direction to choose first.

Note that when I was testing my original workaround on Windows 8.1 and Windows 10, .select()ing a specific item was _not_ collapsing the combobox as it did on Windows 7, so I suspect it might also be necessary with your workaround. I've been developing my tests primarily on Windows 7, and my 8.1/10 test systems aren't available at the moment.

[edit] If I manually invoke the "Close" button when the combobox is expanded, it triggers the appropriate events, but on Windows 7 I have to explicitly re-open the list beforehand; this isn't necessary on 8.1 or 10, where the combobox remains open.

As a side note, the other app is actually primarily ATL (mainly using CAxDialogImpl<> subclasses for its dialogs) - it only uses WTL for the Ribbon control in its main window.

It's worth saying it's possible to check if drop down list is still open with method .exists() which returns True/False instead of raising exception (as it usually happens for non-existing controls or by using .wait('exists')).

P.S. Not sure I'll have a chance to tackle all these issues this or next week.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

rajarameshmamidi picture rajarameshmamidi  路  14Comments

jjbright picture jjbright  路  15Comments

MagazinnikIvan picture MagazinnikIvan  路  14Comments

dstepanenko picture dstepanenko  路  16Comments

mtkennerly picture mtkennerly  路  14Comments