Pyautogui & pywinaut : failed to exception - 2 elements matching the criteria are found

I am getting below error " failed due to exception There are 2 elements that match the criteria {‘title_re’: ‘G326SE6’, ‘control_type’: ‘TreeItem’, ‘top_level_only’: False, ‘enabled_only’: False, ‘visible_only’: False, ‘parent’: <uia_element_info.UIAElementInfo - ‘G326SE6 - PAC Machine Edition’, FrameworXClass, 3870440>, ‘backend’: ‘uia’}"

I don’t see 2 instances of the application on the screen or task manager. Is there any work around to tfix this issue

Hi, I’m the creator of PyAutoGUI. This seems like a pywinaut error. Could you post a link to your code (or if it’s only a few dozen lines, post it directly here) as well as post the full error message with traceback and line numbers? And (this is important), put the source code and error message in "formatted’ text by highlighting it and clicking the </> button in the comment box?

You don’t want your code to look like this:

print(‘Hello, world’)

…but rather, you want it to look like this:

print('Hello, world')

This helps us help you. There isn’t enough information in your post currently to tell us what could be going wrong.

@add_attributes(TCID="TC1000", name="TC_TO_RESTORE1", order=1)
def TC_TO_RESTORE1():
    app = RestartPME()
    if app is None:
        LogMessage(f"Failed to launch PME", False)
        return False
    
    time.sleep(3)
    
    projectName = 'G326SE6'
    targetName = 'G326SE6'
    window_title = 'PAC Machine Edition'
    app = Application(backend="uia").connect(title=window_title)
    main_window = app.window(title=window_title)
    main_window.wait('ready', timeout=10)

    # #Restore Project
    current_directory = os.getcwd()
    project_directory = current_directory + "\\" + "Projects" + "\\" + "G326SE6.zip"
    if os.path.isfile(project_directory) is False:
        LogMessage(f"Project {project_directory} couldn't be found",False)
        return False
    
    result = destroy_project(app, window_title, projectName)
    if result == False:
        LogMessage(f"Destroy project failed",False)
        return False

    result = restore_project(app,main_window,window_title,project_directory, projectName)
    if result == False:
        LogMessage(f"Restore project failed",False)
        return False
    
    # Validate the Target
    result = validate_target(app, main_window,window_title,projectName,targetName, False)[0]
    if result == False:
       print("Target Validation Failed")

Below is the definition for validate_target

def validate_target(app, main_window, window_title, projectName, targetName, validateAll):
    window_title = projectName + ' - PAC Machine Edition'
    if validateAll == True:
        contextMenuItem = 'Validate All'
        selectednode = select_navigation_node(app, window_title, projectName, projectName, False)
    else:
        contextMenuItem = 'Validate\tF7'
        selectednode = select_navigation_node(app, window_title, projectName, targetName, False)
        
    if selectednode is not None:
        draw_context_menu(selectednode)
        result = select_context_menu_item(app, window_title, contextMenuItem)
        return result
    
    return False

And the error is - "
FAIL: TC_TO_RESTORE1 failed due to exception There are 2 elements that match the criteria {‘title_re’: ‘G326SE6’, ‘control_type’: ‘TreeItem’, ‘top_level_only’: False, ‘enabled_only’: False, ‘visible_only’: False, ‘parent’: <uia_element_info.UIAElementInfo - ‘G326SE6 - PAC Machine Edition’, FrameworXClass, 25888182>, ‘backend’: ‘uia’}"

The code seems to find 2 elements for “result = validate_target(app, main_window,window_title,projectName,targetName, False)[0]”

Hmmm, I don’t think I know enough about your project. It’s definitely not an error from PyAutoGUI, though googling for “FAIL: TC_TO_RESTORE” doesn’t come up with anything. Is this your error message that you produce? Also, where is the rest of the code? I don’t see any import statements anywhere, so there must be more to it than this.

Hello,

can you insert a print statement in between the result assignment and the conditional statement from these two:

Insert this:

print(result)

Knowing its value will help in understanding the issue a little better. The conditional statement is expecting the variable result to have a singular value (since it is being compared to a boolean value of False). Is this the case? Can you verify by way of the print statement.

Here is the dictionary that is being highlighted in your post written in different form to enhance readability. Notice how the value for the key parent does not follow the rest of the other values in form. Is this the root cause for the exception?

result = {'title_re': 'G326SE6',
          'control_type': 'TreeItem',
          'top_level_only': False,
          'enabled_only': False,
          'visible_only': False,
          'parent': <uia_element_info.UIAElementInfo - 'G326SE6 - PAC Machine Edition', FrameworXClass, 3870440>,
          'backend': 45}

If the value for result is not a singular value but rather a dictionary, for testing purposes, can you override the actual assumed result dictionary and copy this dictionary as stated here but remove the entry for parent to see if the exception stops being generated.