FoundationAgents/MetaGPT · error · ValueError

Element {element_id} not found

Error message

Element {element_id} not found

What it means

get_backend_node_id in metagpt/utils/a11y_tree.py scans a flattened accessibility-tree list for an entry whose 'nodeId' equals the given element_id (string-compared). If no entry matches, it raises this ValueError, meaning the element id does not exist in the tree snapshot being searched.

Source

Thrown at metagpt/utils/a11y_tree.py:312

    tree_str = dfs(0, accessibility_tree[0]["nodeId"], 0)
    return tree_str, obs_nodes_info


async def get_page_cdp_session(page):
    if hasattr(page, "cdp_session"):
        return page.cdp_session

    cdp_session = await page.context.new_cdp_session(page)
    page.cdp_session = cdp_session
    return cdp_session


def get_backend_node_id(element_id, accessibility_tree):
    element_id = str(element_id)
    for i in accessibility_tree:
        if i["nodeId"] == element_id:
            return i.get("backendDOMNodeId")
    raise ValueError(f"Element {element_id} not found")

View on GitHub (pinned to 11cdf466d0)

Solutions

  1. Regenerate the accessibility tree and re-issue the action with ids from the fresh tree.
  2. Validate the id against the tree before acting: any(i['nodeId'] == str(element_id) for i in tree).
  3. If the LLM produced the id, re-show it the current tree and ask again.
  4. Ensure you pass the same tree object that was used to produce the numbered observation the model saw.

Example fix

# before
backend_id = get_backend_node_id('105', old_tree)  # raises Element 105 not found

# after
from metagpt.utils.a11y_tree import get_backend_node_id
backend_id = get_backend_node_id('12', current_tree)  # id verified present
Defensive patterns

Strategy: validation

Validate before calling

def element_id_exists(element_id, accessibility_tree) -> bool:
    element_id = str(element_id)
    return any(i["nodeId"] == element_id for i in accessibility_tree)

Try / catch

try:
    backend_id = get_backend_node_id(element_id, tree)
except ValueError:
    tree = await get_accessibility_tree(page)  # resync
    backend_id = get_backend_node_id(element_id, tree)

Prevention

When it happens

Trigger: Passing an element id from an older tree after the page changed; passing a numeric id when the tree stores string nodeIds (the function does str() so this is fine) but passing an id the LLM hallucinated; searching tree A while the id came from tree B; the id is a selector/CSS string rather than a nodeId.

Common situations: LLM invents or mis-transcribes an element id (e.g. '[105]' when the tree ends at [98]); the page mutated between observation and action so the id is gone; multiple pages/tabs with the wrong tree passed in.

Related errors


AI-assisted analysis of FoundationAgents/MetaGPT@11cdf466d0 (2026-08-14). Data as JSON: /api/errors/b70f6e3a83d1e732. Report an issue: GitHub.