{"record":{"id":"b70f6e3a83d1e732","repo":"FoundationAgents/MetaGPT","slug":"element-element-id-not-found","errorCode":null,"errorMessage":"Element {element_id} not found","messagePattern":"Element (.+?) not found","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"metagpt/utils/a11y_tree.py","lineNumber":312,"sourceCode":"    tree_str = dfs(0, accessibility_tree[0][\"nodeId\"], 0)\n    return tree_str, obs_nodes_info\n\n\nasync def get_page_cdp_session(page):\n    if hasattr(page, \"cdp_session\"):\n        return page.cdp_session\n\n    cdp_session = await page.context.new_cdp_session(page)\n    page.cdp_session = cdp_session\n    return cdp_session\n\n\ndef get_backend_node_id(element_id, accessibility_tree):\n    element_id = str(element_id)\n    for i in accessibility_tree:\n        if i[\"nodeId\"] == element_id:\n            return i.get(\"backendDOMNodeId\")\n    raise ValueError(f\"Element {element_id} not found\")\n","sourceCodeStart":294,"sourceCodeEnd":313,"githubUrl":"https://github.com/FoundationAgents/MetaGPT/blob/11cdf466d042aece04fc6cfd13b28e1a70341b1f/metagpt/utils/a11y_tree.py#L294-L313","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Regenerate the accessibility tree and re-issue the action with ids from the fresh tree.","Validate the id against the tree before acting: any(i['nodeId'] == str(element_id) for i in tree).","If the LLM produced the id, re-show it the current tree and ask again.","Ensure you pass the same tree object that was used to produce the numbered observation the model saw."],"exampleFix":"# before\nbackend_id = get_backend_node_id('105', old_tree)  # raises Element 105 not found\n\n# after\nfrom metagpt.utils.a11y_tree import get_backend_node_id\nbackend_id = get_backend_node_id('12', current_tree)  # id verified present","handlingStrategy":"validation","validationCode":"def element_id_exists(element_id, accessibility_tree) -> bool:\n    element_id = str(element_id)\n    return any(i[\"nodeId\"] == element_id for i in accessibility_tree)","typeGuard":null,"tryCatchPattern":"try:\n    backend_id = get_backend_node_id(element_id, tree)\nexcept ValueError:\n    tree = await get_accessibility_tree(page)  # resync\n    backend_id = get_backend_node_id(element_id, tree)","preventionTips":["Use the exact tree the LLM saw as the numbered observation.","Re-show the tree to the model when it references an id not present.","Convert ids to str before comparing; the lookup is string-based."],"tags":["metagpt","accessibility-tree","llm-output","stale-reference","validation"],"backgroundTag":null,"analyzedSha":"11cdf466d042aece04fc6cfd13b28e1a70341b1f","analyzedAt":"2026-08-14T23:20:02.994Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}