Search code examples
pythonpython-3.xautomationui-automationpywinauto

How to identify UI elements when multiple UI elements have same UI elements


I am trying UI automation using PywinAuto Lib. When I am trying, Multiple UI elements are having same UI Attributes . Is there any way , we can alias and identify what we need.

ControlType: RadioButtonControl    ClassName: RadioButton    AutomationId: checkBox1    Rect: (805, 259, 855, 287)    Name:     Handle: 0x0(0)    Depth: 7    CurrentIsSelected: True

ControlType: RadioButtonControl    ClassName: RadioButton    AutomationId: checkBox1    Rect: (858, 259, 908, 287)    Name:     Handle: 0x0(0)    Depth: 7    CurrentIsSelected: False

As we have see , we are seeing same UI attributes for both the UI elements. One way of deal is, we can go back to UI development and change the things .

But is there any way we can do alias and identify .

selectButton = app1.Dialog.child_window(auto_id="checkBox1",control_type="RadioButton")
selectButton .click_input(button='left')

when we run , we are seeing this error .

pywinauto.findwindows.ElementAmbiguousError: There are 3 elements that match the criteria {'auto_id': 'checkBox1', 'control_type': 'RadioButton', 'top_level_only': False, 'parent': <uia_element_info.UIAElementInfo - 'Ui automation', Window, 339012>, 'backend': 'uia'}

Solution

  • There are few ways:

    1. Using found_index=i in search criteria of child_window.

    2. Using children() but there is very limited number of filter criteria in 0.6.x (planned for future improvements): class_name, control_type, content_only, title.

    3. Using best_match search rules from Getting Started Guide -> How to know magic attribute names (previous chapters are recommended to read as well).