Search code examples
pythoncomboboxpywinauto

how to select a combobox without a title using pywinauto


In my windows application there are three ComboBoxes without title but different list items. How can I select right ComboBox based on the items it contains. If I only select Control_Type I am getting ElementAmbiguousError.

I am getting error :-

pywinauto.findwindows.ElementAmbiguousError: There are 3 elements that match the criteria {'control_type': 'ComboBox', 'top_level_only': False, 'parent': <uia_element_info.UIAElementInfo - 'ApplicationName', MetroWindow, 591164>, 'backend': 'uia'}


from pywinauto.application import Application
from pywinauto.mouse import click
import time


# Path to the application executable
app_path = r"file path"

# Start the application
app = Application(backend='uia').start(app_path)

# Wait for the main window to appear (adjust timeout as needed)
main_window = app.window(title_re="ApplicationName")
# Now you can interact with the main window or its controls as needed
# Example: print the main window's text
print(main_window.window_text())

# Example: click a button in the main window
main_window.child_window(title="System.Windows.Controls.ListViewItem", auto_id="Alarm", control_type="ListItem").click_input()
time.sleep(1)

main_window.child_window(control_type="ComboBox").click_input()

Solution

  • The workaround is simply using found_index in search criteria:

    main_window.child_window(control_type="ComboBox", found_index=0).click_input()
    

    For your case the index could be 0, 1 or 2. You can double check which combobox you've found by method .draw_outline() instead of .click_input().