Four DOM Traps That Make Browser Agents Click or Fill the Wrong Element
Last week I ran an AI coding agent overnight across Gmail, Wikipedia, Reddit, and Dover ATS forms through turbo-browser-mcp, my Playwright MCP server. All 750 local fixture tests were green before the run started. By mor
Last week I ran an AI coding agent overnight across Gmail, Wikipedia, Reddit, and Dover ATS forms through turbo-browser-mcp, my Playwright MCP server. All 750 local fixture tests were green before the run started. By morning, real production DOMs had broken element targeting in four ways that synthetic fixtures missed.
1. Recycled DOM nodes in virtualized lists
Most browser MCP servers assign a reference attribute like data-tbm-ref="e4" during a snapshot so the model can pass ref: "f0:e4" on the next turn. In a normal SPA re-render, a replaced subtree drops the attribute and a stale ref fails cleanly.
Virtualized lists (react-window, Gmail thread tables, infinite feeds) do something different: when the viewport scrolls, they keep the exact same DOM node and swap its text and event handlers for a new row. The data-tbm-ref="e4" attribute survives on the recycled node, so a stale ref clicks Row 14 when the model intended to click Row 0.
We fixed this in two layers:
- For buttons, links, and clickable rows, each issued ref records
(frameLoaderId, first 128 chars of normalized textContent). If either changes before the action runs, the tool refuses the ref and asks for a freshinspect_compact. - For value controls (
<input>,<textarea>,<select>,contenteditable), exempting the value is required so typing twice into the same field works, while exempting the identity is a bug. Each value control is stamped with#id:plustag,type,name,id,aria-label,placeholder,role,title, and resolved<label>text (with live character counters like(12/160)normalized to(#/#)).
2. Cloned form inputs when sections reorder or delete
In a review thread on my previous post, Renhua Wei pointed out a gap in attribute stamps: repeated fields like three "Phone" inputs or multi-line address rows share the exact same tag, type, name, and aria-label. If two cloned rows swap positions, an attribute-only stamp still matches.
Appending the element's 0-based index among identical clones solves a straight swap, until the user deletes row 1 of 3. Then row 2 shifts from index 1 to 0 and row 3 shifts from 2 to 1, breaking every surviving ref in the form.
The fix that survived both cases (ba47c55 and 3446e79) scopes the clone group to the nearest ancestor holding at least two matching clones and records index/total (0/3, 1/3, 2/3):
// When group size stays 3 (a reorder or swap), an index change is a stale ref error.
// When group size changes (3 -> 2 after deleting a row), shift by (curTotal - storedTotal)
// and attach a one-time note so surviving refs still fill the right inputs.
That scoping has an important consequence depending on form layout:
- If two
<fieldset>sections each hold three identicalPhoneinputs, the nearest clone-holding ancestor is the<fieldset>. Swapping the two fieldsets wholesale withinsertBeforekeeps every input at0/3,1/3,2/3inside its own fieldset, so all six refs stay valid. - If you flip the layout to three unlabelled
<fieldset>elements with onePhoneinput each, the nearest clone-holding ancestor falls back to the<form>. Swapping the first two fieldsets flips their indices inside the form (0/3 <-> 1/3) and throws a stale-ref error (test/night-19-11-clone-shift.test.mjs).
3. Hydration mutating title attributes after the snapshot
On Tuesday night our automated real-site run hit Wikipedia (en.wikipedia.org). Right after inspect_compact returned the search box ref, every act_fast call to type into that search box failed with now points to a different field.
Reproducing it in a standalone script (docs/repro_wiki_search.mjs) showed why: at initial domcontentloaded, Wikipedia's search input has title="Search Wikipedia [f]". A few hundred milliseconds later, client-side JavaScript rewrites the access-key hint to match the platform modifier: title="Search Wikipedia [alt-f]" (or [alt-shift-f]).
Because title was part of our identity stamp, the late access-key rewrite invalidated the ref between the snapshot turn and the typing turn. Stripping trailing [...] access-key suffixes when normalizing title (c140e2d) fixed Wikipedia and MediaWiki sites without weakening actual title checks.
4. Chrome freezing background tabs and repeating CDP prompts
When an agent drives your real logged-in Chrome over CDP while you work in another tab, two browser behaviors get in the way:
First, opening a new tab brings it to the foreground and steals keyboard focus. I opened a PR upstream to Google's official ChromeDevTools/chrome-devtools-mcp#3539 adding a --backgroundNewPages server flag so new_page keeps tabs in the background even when the model omits background: true (merged into main yesterday).
Second, once a tab stays in the background, Chrome throttles its lifecycle and stops running animation-frame rendering. When our agent opened Gmail in a background tab, the compose editor never mounted until someone clicked the tab. Pairing background tab creation with CDP Emulation.setFocusEmulationEnabled({ enabled: true }) and Page.setWebLifecycleState({ state: "active" }) (d1e755f) keeps heavy SPAs rendering in the background while your active tab stays untouched.
All four fixes ship in turbo-browser-mcp (778 tests across Node 20 and 22):
https://github.com/Tthanh2k6/turbo-browser-mcp
Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes β full credit and traffic to the original publisher.