Skip to content

fix(snapshot): resolve DevTools selected element in take_snapshot output - #2293

Closed
syf2211 wants to merge 2 commits into
ChromeDevTools:mainfrom
syf2211:fix/devtools-selected-element-snapshot
Closed

fix(snapshot): resolve DevTools selected element in take_snapshot output#2293
syf2211 wants to merge 2 commits into
ChromeDevTools:mainfrom
syf2211:fix/devtools-selected-element-snapshot

Conversation

@syf2211

@syf2211 syf2211 commented Jul 4, 2026

Copy link
Copy Markdown

Summary

Fix take_snapshot so the element selected in the Chrome DevTools Elements panel is exposed with a stable uid and [selected in the DevTools Elements panel] marker.

Motivation

Fixes #2243. When users select an element in DevTools and ask an agent to inspect it, the snapshot reported that an element was selected but did not identify which one. This happened because:

  1. selectedElementUid was resolved against the previous textSnapshot, not the snapshot being built.
  2. Elements outside the accessibility tree were never inserted, even in verbose mode.

Changes

  • Resolve selectedElementUid from the in-progress snapshot tree (idToNode).
  • When the selected element is missing from the a11y tree, resolve it via adoptBackendNode and insert it with the existing insertExtraNodes path.
  • Restore page.extraHandles after the temporary selected-element insertion to avoid leaking state across snapshots.
  • Update the unresolved-selection note to stop recommending verbose mode (which does not fix this case).

Tests

  • node scripts/test.mjs tests/TextSnapshot.test.ts tests/formatters/snapshotFormatter.test.ts — all pass
  • npm run check-format — pass

Notes

  • Selection inside iframes or closed shadow roots may still fail to resolve; the note now explains likely causes.
  • Google CLA signature may be required for this repository.

When an element is selected in the DevTools Elements panel, take_snapshot
now resolves selectedElementUid against the snapshot being built instead
of the previous textSnapshot. If the selected node is missing from the
a11y tree, insert it via adoptBackendNode so agents can read its uid and
properties.

Fixes ChromeDevTools#2243
@google-cla

google-cla Bot commented Jul 4, 2026

Copy link
Copy Markdown

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@syf2211

syf2211 commented Jul 4, 2026

Copy link
Copy Markdown
Author

I have read the CLA Document and I hereby sign the CLA

@syf2211

syf2211 commented Jul 4, 2026

Copy link
Copy Markdown
Author

recheck

@syf2211

syf2211 commented Jul 4, 2026

Copy link
Copy Markdown
Author

recheck

1 similar comment
@syf2211

syf2211 commented Jul 8, 2026

Copy link
Copy Markdown
Author

recheck

@syf2211

syf2211 commented Jul 8, 2026

Copy link
Copy Markdown
Author

I have read the CLA Document and I hereby sign the CLA

@syf2211

syf2211 commented Jul 8, 2026

Copy link
Copy Markdown
Author

recheck

@nattallius

Copy link
Copy Markdown
Contributor

Hi, thank you for you effort, but that issue has already been solved in #2295 . Closing as duplicate

@nattallius nattallius closed this Jul 9, 2026
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.

[Bug/Feature Request] Unable to retrieve ID of element selected in Chrome DevTools via MCP Snapshot tool

2 participants