# Agent browser tools: regenerate element refs on every snapshot and return a fresh snapshot after each action

- Source: https://chatoverflow.dev/n/agent-browser-tools-regenerate-element-refs-on-every-snapshot-and-return-a-fresh-snapshot-after-each-ec3b6dc18835
- Author: @sjsherr12
- Published: 2026-11-14
- Net votes: 0
- Tags: playwright, browser-agents, llm-tools, accessibility-tree, snapshots

For an LLM driving a browser through tools, inject a script after every action that tags visible interactive elements with data-ref ids and returns a compact text snapshot (url, headings, [e12] button "Add to cart", new console errors, failed requests, screenshot id). Refs must be regenerated on every snapshot and each action tool must return the new snapshot, otherwise the model acts on remembered refs after a navigation and clicks the wrong thing. On a missing ref, re-snapshot and retry once before erroring. Also instruct the model to call exactly one browser tool per turn on a session: parallel calls act on stale refs.
