Skip to content

Bug Report: Invalid XPath Generation (/button) Due to backend_node_id=None #243

@subhiandrews

Description

@subhiandrews

Before submitting an issue, please:

Environment Information

Please provide the following information to help us reproduce and resolve your issue:

Stagehand:

  • Language/SDK: Python
  • Stagehand version: 0.5.7

AI Provider:

  • Provider: Vertex AI
  • Model: gemini-2.5-flash

Issue Description

Expected Behavior

When the AI correctly identifies an element in the accessibility tree (e.g., element_id: 153 for a "Sign in" button), Stagehand should generate a valid XPath selector like:
xpath=/html/body[1]/nav[1]/div[1]/button[1]

Actual Behavior

Stagehand generates an invalid root-level XPath:
xpath=/button

This XPath doesn't exist in the DOM (buttons are never at the document root), causing a 30-second timeout.

Steps to Reproduce

Steps to Reproduce

  1. Install dependencies:
    pip install stagehand==0.5.7 playwright
    playwright install

  2. Create test script (test_stagehand_bug.py):
    import asyncio
    from stagehand import Stagehand

async def test_sign_in_button():
# Initialize Stagehand with Vertex AI
stagehand = Stagehand(
env="LOCAL",
verbose=2,
debugDom=True,
modelName="gemini-2.5-flash",
modelClientOptions={
"project": "YOUR_GCP_PROJECT_ID",
"location": "us-east1"
}
)

  await stagehand.init()
  page = stagehand.page

  # Navigate to ID.me homepage
  await page.goto("https://www.id.me")
  await page.wait_for_load_state("networkidle")
  await page.wait_for_timeout(3000)

  # Dismiss cookie banner (this works!)
  await page.act("click the Accept button in the cookie consent banner")
  await page.wait_for_timeout(1000)

  # Try to click Sign In button (this fails!)
  await page.act("click the Sign In button in the top navigation")

  await stagehand.close()

asyncio.run(test_sign_in_button())

  1. Run the script:
    python test_stagehand_bug.py

Error Messages / Log trace

✅ Working Example (Cookie "Accept" Button)

[Stagehand] LLM Response: {
"elements": [{
"element_id": 99,
"description": "Accept button in the cookie consent banner",
"method": "click",
"arguments": []
}]
}
[Stagehand] Getting xpath for element 99
[Stagehand] Found elements: [ObserveResult(
selector='xpath=/html/body[1]/div[1]/div[2]/div[2]/button[1]', # ✅ VALID XPATH
description='Accept button in the cookie consent banner',
backend_node_id=None,
method='click',
arguments=[]
)]
[Stagehand] click complete # ✅ SUCCESS

❌ Failing Example (Sign In Button)

[Stagehand] LLM Response: {
"elements": [{
"element_id": 153,
"description": "Sign in button",
"method": "click",
"arguments": []
}]
}
[Stagehand] Getting xpath for element 153
[Stagehand] Found elements: [ObserveResult(
selector='xpath=/button', # ❌ INVALID XPATH
description='Sign in button',
backend_node_id=None, # ❌ ROOT CAUSE
method='click',
arguments=[]
)]
[Stagehand] error performing click: Locator.evaluate: Timeout 30000ms exceeded.
Call log:
- waiting for locator("xpath=/button").first

Accessibility Tree Context (element 153 exists and is correct):
[127] navigation: Site
[129] list
...
[27] div
[145] link: Help Center
[153] button: Sign in ← AI correctly identifies this element

Analysis

  1. AI Element Selection: ✅ Working correctly - identifies element 153
  2. XPath Generation: ❌ Broken - produces /button instead of proper path
  3. backend_node_id: ❌ Always None for both working and failing cases
  4. Pattern: Some elements (like cookie banner buttons) generate valid XPaths despite backend_node_id=None, while others (like navigation buttons) generate invalid root-level XPaths

###Impact

This bug makes Stagehand unreliable for interacting with navigation elements and other buttons that aren't in simple DOM structures. It's a blocker for using Stagehand in production test automation.

Attempted Workarounds

  • ✅ Tried different Gemini models (gemini-2.5-flash, gemini-1.5-pro)
  • ✅ Used useVision=True parameter (still generates invalid XPath internally)
  • ✅ Increased domSettleTimeoutMs to 30000
  • ✅ Upgraded from 0.5.6 to 0.5.7
  • ✅ Added explicit waits before interactions
  • ❌ None of these resolved the issue

###Related Issues

This appears related to the TypeScript version's issue:

Are there any related issues or PRs?

  • Related to: #[issue number]
  • Duplicate of: #[issue number]
  • Blocks: #[issue number]

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions