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

**URL:** <https://discuss.python.org/t/pyautogui-pywinaut-failed-to-exception-2-elements-matching-the-criteria-are-found/85769>\
**Category:** Python Help\
**Tags:** help\
**Created:** [March 24, 2025, 10:04am UTC](https://discuss.python.org/t/pyautogui-pywinaut-failed-to-exception-2-elements-matching-the-criteria-are-found/85769 "2025-03-24T10:04:08Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![pgokave](https://avatars.discourse-cdn.com/v4/letter/p/67e7ee/32.png) [@pgokave](https://discuss.python.org/u/pgokave)\
**Post date:** [March 24, 2025, 10:04am UTC](https://discuss.python.org/t/pyautogui-pywinaut-failed-to-exception-2-elements-matching-the-criteria-are-found/85769/1 "2025-03-24T10:04:08Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![AlSweigart](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/alsweigart/32/12237_2.png) [@AlSweigart](https://discuss.python.org/u/AlSweigart)\
**Post date:** [March 24, 2025, 3:56pm UTC](https://discuss.python.org/t/pyautogui-pywinaut-failed-to-exception-2-elements-matching-the-criteria-are-found/85769/2 "2025-03-24T15:56:51Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![pgokave](https://avatars.discourse-cdn.com/v4/letter/p/67e7ee/32.png) [@pgokave](https://discuss.python.org/u/pgokave)\
**Post date:** [March 27, 2025, 3:44am UTC](https://discuss.python.org/t/pyautogui-pywinaut-failed-to-exception-2-elements-matching-the-criteria-are-found/85769/3 "2025-03-27T03:44:45Z")

</div>

```python
@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

```python
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]”

---

<div class="post-metadata">

**Author:** ![AlSweigart](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/alsweigart/32/12237_2.png) [@AlSweigart](https://discuss.python.org/u/AlSweigart)\
**Post date:** [April 1, 2025, 4:42pm UTC](https://discuss.python.org/t/pyautogui-pywinaut-failed-to-exception-2-elements-matching-the-criteria-are-found/85769/4 "2025-04-01T16:42:29Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![onePythonUser](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/onepythonuser/32/19158_2.png) [@onePythonUser](https://discuss.python.org/u/onePythonUser)\
**Post date:** [April 2, 2025, 3:33am UTC](https://discuss.python.org/t/pyautogui-pywinaut-failed-to-exception-2-elements-matching-the-criteria-are-found/85769/5 "2025-04-02T03:33:22Z")

</div>

Hello,

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

> [@pgokave](#):
>
> ```python
> # Validate the Target
> result = validate_target(app, main_window,window_title,projectName,targetName, False)[0]
> if result == False:
> print("Target Validation Failed")
> 
> ```

Insert this:

```python
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?

```python
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.
