Skip to content

feat: support element-scoped accessibility snapshots - #3506

Merged
wolfib merged 3 commits into
ChromeDevTools:mainfrom
lisa0314:feat/716-snapshot-root
Dec 3, 2026
Merged

wolfib merged 3 commits into
ChromeDevTools:mainfrom
lisa0314:feat/716-snapshot-root

Conversation

@lisa0314

@lisa0314 lisa0314 commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #716.

take_snapshot accepts an optional Puppeteer selector (CSS, ::-p-text, ::-p-xpath, or >>>) for the first matching main-frame element. The tool takes the usual full-page accessibility snapshot, including iframe documents, then filters the inline, structured, or file output to the selected subtree. Other tools can still resolve UIDs anywhere on the page. The DevTools selection marker is scoped to the output, and compact mode suggests a verbose snapshot when it prunes the selected element inside the subtree. Omitting the selector returns the full page.

Scoped output respects verbose. If compact mode prunes a container, its visible accessibility descendants are grouped under a UID-less container. A missing match or a subtree without accessibility content produces an error. Selector traversal includes shadow roots and propagates protocol errors from DOM.describeNode. Whitespace-only selectors fail schema validation.

The CLI, tool reference, and tool-selection guidance document the selector. The separate fix for Puppeteer's handling of multiple promoted descendants in its rooted snapshot API is tracked in puppeteer/puppeteer#15884.

@OrKoN
OrKoN self-requested a review November 17, 2026 19:58
Comment thread src/TextSnapshot.ts Outdated
page.pptrPage.accessibility.snapshot({
includeIframes: true,
interestingOnly: !verbose,
...(root ? {root} : {}),

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it make sense to always take full snapshot and filter it down according to the root param?

@OrKoN OrKoN left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Additionally please check this:

  1. Honor verbose for scoped snapshots (src/TextSnapshot.ts, interestingOnly: !root && !verbose). A scoped snapshot always returns the full tree, ignores verbose, and still sets TextSnapshot.verbose to false, which reduces the token saving that #716 asks for.

  2. Fix the root cause in Puppeteer first (Accessibility.snapshot in puppeteer-core). With root and interestingOnly: true, serializeTree(needle, interestingNodes)[0] returns only the first interesting descendant when the root is not interesting, and AGENTS.md requires an upstream fix instead of a local workaround.

  3. Take full snapshots and filter output based on the selector instead of taking partial snapshot.

  4. State the selector syntax in the selector description (src/tools/snapshot.ts). page.$ accepts Puppeteer selectors (::-p-xpath, ::-p-text, >>>), not only CSS, and the description does not tell the agent which syntax is valid.

  5. Reject whitespace-only selectors in the schema (src/tools/snapshot.ts, use .trim().min(1)). min(1) accepts " ", and page.$ then throws a raw selector syntax error instead of a validation error.

  6. Replace the real-browser test in tests/McpResponse.test.ts with a mock test that asserts selector reaches TextSnapshot.create. The test runs five scenarios in one browser session, and the mock tests in tests/TextSnapshot.test.ts already cover the missing-selector and root-option branches.

  7. Call findAncestorNode one time per handle (src/TextSnapshot.ts, #insertExtraNodes). The expression ancestor || (await findAncestorNode(handle)) || rootNodeWithId hides that the scoped and full-page paths use different call orders.

  8. Rebase on main and resolve the conflict in tests/TextSnapshot.test.ts. main changed the extra-handle code in #3493, so the new extra-handle test must run against that version.

@OrKoN OrKoN left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please check the following:

  1. Make resolveCdpElementId find nodes outside the selected subtree (src/TextSnapshot.ts; use idToNode, and use a separate subtree check for selectedElementUid). The method walks this.root, so after a scoped snapshot get_css_styles container queries and issue details in get_console_message print no UID for those nodes, and McpPage.resolveBackendNodeId takes a second full snapshot.

  2. Remove the resolveCdpElementId(outsideId) === undefined assertion in keeps retained extra handles outside the selected subtree out of the snapshot (tests/TextSnapshot.test.ts). The assertion makes the behavior from item 1 a requirement, and it contradicts the PR description sentence "The full UID lookup remains available".

  3. Add mock tests for the grouped-container branch and for the No accessibility nodes for selector error (tests/TextSnapshot.test.ts). Only one real-browser test reaches the else branch, it asserts with regular expressions on JSON, and no test reaches the error.

  4. Run does not suggest verbose mode for a selected element outside the subtree with verbose: false (tests/TextSnapshot.test.ts). SnapshotFormatter prints the note only when verbose is false, so the test with verbose: true does not cover the case in its name.

  5. Move the shadowRoots traversal in findDescendantNodes to a separate PR, or add a test and list it in the PR description (src/TextSnapshot.ts). The change also applies to #insertExtraNodes, so extra handles now re-parent shadow-DOM descendants, and no test or description text covers that.

  6. Throw the DOM.describeNode failure for the selector path instead of the No accessibility nodes error (src/TextSnapshot.ts, findDescendantNodes). The catch returns an empty set, so a CDP failure reports a wrong cause to the agent.

  7. Keep hasSelectedElement true when compact mode prunes a DevTools-selected element that is inside the selected subtree (src/TextSnapshot.ts, !root || Boolean(snapshot.selectedElementUid)). The expression also removes the "get a verbose snapshot" note for that element, and a verbose scoped snapshot does include it.

@lisa0314

lisa0314 commented Dec 3, 2026

Copy link
Copy Markdown
Contributor Author

Updated in a629491. resolveCdpElementId now uses the full-page UID map so other tools can still resolve nodes outside the snapshot subtree. The DevTools selection marker and verbose hint follow the selected subtree, including when compact mode prunes the selected element.

Shadow-root traversal is now limited to selector lookups, which also propagate DOM.describeNode errors. Added mock coverage for grouped children, empty subtrees, and protocol failures.

@wolfib wolfib left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@wolfib
wolfib merged commit f1b1719 into ChromeDevTools:main Dec 3, 2026
15 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Accessibility tree/snapshot for a specific DOM element, including whole hierarchy below it (custom "root" element)

3 participants