FoundationAgents/MetaGPT · error · ValueError

Invalid click action {step}

Error message

Invalid click action {step}

What it means

ValueError from execute_step in metagpt/utils/a11y_tree.py: the step string starts with 'click' but does not match the expected grammar `click [N]` where N is one or more digits (regex `click ?\[(\d+)\]`). The action word is recognized but its argument is malformed, so the browser action is rejected before executing.

Source

Thrown at metagpt/utils/a11y_tree.py:31

    seen_ids = set()
    accessibility_tree = []
    for node in resp["nodes"]:
        if node["nodeId"] not in seen_ids:
            accessibility_tree.append(node)
            seen_ids.add(node["nodeId"])
    return accessibility_tree


async def execute_step(step: str, page: Page, browser_ctx: BrowserContext, accessibility_tree: list):
    step = step.strip()
    func = step.split("[")[0].strip() if "[" in step else step.split()[0].strip()
    if func == "None":
        return ""
    elif func == "click":
        match = re.search(r"click ?\[(\d+)\]", step)
        if not match:
            raise ValueError(f"Invalid click action {step}")
        element_id = match.group(1)
        await click_element(page, get_backend_node_id(element_id, accessibility_tree))
    elif func == "hover":
        match = re.search(r"hover ?\[(\d+)\]", step)
        if not match:
            raise ValueError(f"Invalid hover action {step}")
        element_id = match.group(1)
        await hover_element(page, get_backend_node_id(element_id, accessibility_tree))
    elif func == "type":
        # add default enter flag
        if not (step.endswith("[0]") or step.endswith("[1]")):
            step += " [1]"

        match = re.search(r"type ?\[(\d+)\] ?\[(.+)\] ?\[(\d+)\]", step)
        if not match:
            raise ValueError(f"Invalid type action {step}")
        element_id, text, enter_flag = (
            match.group(1),

View on GitHub (pinned to 11cdf466d0)

Solutions

  1. Emit steps in the exact form `click [12]` using numeric nodeIds taken from the printed accessibility tree
  2. Validate agent output with the regex re.search(r'click ?\[(\d+)\]', step) before calling execute_step
  3. Tighten the agent prompt/action space so only well-formed actions are generated

Example fix

# before
await execute_step("click [submit_button]", page, ctx, tree)  # ValueError
# after
await execute_step("click [42]", page, ctx, tree)  # 42 = nodeId from a11y tree
Defensive patterns

Strategy: validation

Validate before calling

import re
if not re.search(r"click ?\[(\d+)\]", step):
    raise SystemExit(f"malformed click step: {step!r}; expected 'click [id]'")

Type guard

def is_valid_click(step: str) -> bool:
    import re
    return bool(re.fullmatch(r"click ?\[\d+\]", step.strip()))

Try / catch

try:
    await execute_step(step, page, ctx, tree)
except ValueError:
    step = f"click [{node_id}]"  # regenerate from a known-good nodeId
    await execute_step(step, page, ctx, tree)

Prevention

When it happens

Trigger: Steps like 'click [ok]' (non-numeric id), 'click[12' (missing bracket), 'click []', or 'click 12' without brackets — anything where the click regex fails to match.

Common situations: LLM web agent emits free-form or hallucinated element ids instead of numeric node ids from the accessibility tree; prompt format drift between model output and the strict grammar; copy-paste of steps from a different browser-agent framework.

Related errors


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