Lesson 39.2 · 2 of 5 · 4 min read · Reviewed 24 Nov 2026
Screenshots vs accessibility trees vs DOM
By the end, you can
- Compare screenshots, accessibility trees and the DOM as inputs.
- Choose the input that fits the app.
An agentAgent: A system where a model decides its own next steps, calling tools in a loop until the task is done. can “see” a screen in several ways.
| Input | Works on | Strengths | Weaknesses |
|---|---|---|---|
| Screenshot | Anything | Universal; sees what users see | Needs visual reasoning; clicks by coordinates |
| Accessibility tree | Most native and web apps | Named roles and labels (“button: Submit”) | Missing or wrong labels in some apps |
| DOM | Web pages | Exact elements and attributes | Huge and noisy; needs trimming |
Combining them
Many browser agentsBrowser agent: An agent that navigates websites by reading pages and clicking, typing and scrolling. use a screenshot for layout and context, plus a trimmed list of interactive elements with ids. The model then says “click element 14” instead of “click at (512, 388)”, which is far more reliable.
Practical tips
- Keep the screen size moderate and fixed. Very large screens make small targets harder to hit.
- Trim the DOM to visible, interactive elements, or it will flood the context.
- Accessibility labels help agents and screen-reader users alike, one more reason to build accessible apps.
Key points
- Screenshots work on any app but need visual reasoning and precise coordinates.
- Accessibility trees give named, structured elements for native and web apps.
- In browsers, the DOM (or a cleaned-up version of it) allows precise targeting by element.
Trim a page to clickable elements
Turn a noisy page into a short numbered list of visible, interactive elements that an agent can click by id.
You will have: A trimming function, the numbered element list it produces, and the size saving compared with the raw page.
Steps
- Read the simulated DOM: a list of elements with tag, text, role, visibility and a bounding box.
- Keep only visible elements that are interactive: links, buttons, inputs, selects, or anything with a button role.
- Number them and render one short line each, such as [3] button "Submit claim".
- Write click(element_id) that turns an id back into the centre of its box.
- Print the list, the size saving, and the coordinates for one click.
Done when
- Hidden elements and plain text containers are gone.
- The numbered list is at least 5 times smaller than the raw DOM text.
- click(3) returns the centre of the Submit claim button.
Open in the Playground No API key needed.
Starter code (Python)
import json
dom = [
{"tag": "div", "text": "Header", "visible": True, "box": [0, 0, 1024, 60]},
{"tag": "a", "text": "Home", "visible": True, "box": [10, 10, 60, 30]},
{"tag": "input", "text": "", "label": "Amount", "visible": True, "box": [100, 120, 300, 150]},
{"tag": "div", "text": "Tracking pixel", "visible": False, "box": [0, 0, 1, 1]},
{"tag": "select", "text": "", "label": "Category", "visible": True, "box": [100, 170, 300, 200]},
{"tag": "div", "text": "Submit claim", "role": "button", "visible": True, "box": [100, 230, 240, 270]},
{"tag": "button", "text": "Cancel", "visible": False, "box": [260, 230, 340, 270]},
] + [{"tag": "span", "text": "Footer note " * 20, "visible": True, "box": [0, 700, 1024, 768]}] * 10
INTERACTIVE = {"a", "button", "input", "select"}
def trim(dom):
# TODO: keep visible, interactive elements (by tag or role == "button"); number them from 0
return []
elements = trim(dom)
for i, e in enumerate(elements):
print(f"[{i}] {e['tag']} {e.get('text') or e.get('label')!r}")
Show a solution
import json
dom = [
{"tag": "div", "text": "Header", "visible": True, "box": [0, 0, 1024, 60]},
{"tag": "a", "text": "Home", "visible": True, "box": [10, 10, 60, 30]},
{"tag": "input", "text": "", "label": "Amount", "visible": True, "box": [100, 120, 300, 150]},
{"tag": "div", "text": "Tracking pixel", "visible": False, "box": [0, 0, 1, 1]},
{"tag": "select", "text": "", "label": "Category", "visible": True, "box": [100, 170, 300, 200]},
{"tag": "div", "text": "Submit claim", "role": "button", "visible": True, "box": [100, 230, 240, 270]},
{"tag": "button", "text": "Cancel", "visible": False, "box": [260, 230, 340, 270]},
] + [{"tag": "span", "text": "Footer note " * 20, "visible": True, "box": [0, 700, 1024, 768]}] * 10
INTERACTIVE = {"a", "button", "input", "select"}
def trim(dom):
return [e for e in dom if e["visible"] and (e["tag"] in INTERACTIVE or e.get("role") == "button")]
def render(elements):
return "\n".join(f"[{i}] {e.get('role', e['tag'])} {e.get('text') or e.get('label')!r}" for i, e in enumerate(elements))
def click(elements, i):
x1, y1, x2, y2 = elements[i]["box"]
return ((x1 + x2) // 2, (y1 + y2) // 2)
elements = trim(dom)
listing = render(elements)
print(listing)
raw = len(json.dumps(dom))
print(f"size: raw={raw} trimmed={len(listing)} ({raw / len(listing):.0f}x smaller)")
print("click [3] at", click(elements, 3))