snapshot -s duplicates some elements and drops others
snapshot -s prints certain elements inside the selected area multiple times. This is behavior is reported in #1338 and #1448. It also drops what's in a custom element's shadow root, which is reported as part 2 of #1660.
Repro
agent-browser 0.38.1 with the bundled Chrome 151.
Duplicated elements
<main>
<div><div>
<div role="tablist"><button role="tab" aria-selected="true">Overview</button><button role="tab">Settings</button></div>
</div></div>
<div class="bar"><div><button>Help</button></div></div>
<p>Plain <b>text</b> here</p>
</main>
<footer><button>Contact</button></footer>agent-browser snapshot -s main gives:
- main
- tablist
- tab "Overview" [selected, ref=e2]
- tab "Settings" [ref=e3]
- button "Help" [ref=e4]
- paragraph
- StaticText "Plain text here"
- tablist
- tab "Overview" [selected, ref=e2]
- tab "Settings" [ref=e3]
- button "Help" [ref=e4]A select does it too:
<main><label>Size <select><option>Small</option><option>Large</option></select></label></main>agent-browser snapshot -s main gives:
- main
- LabelText
- StaticText "Size "
- combobox "Size " [expanded=false, ref=e5]: Small
- MenuListPopup
- option "Small" [selected, ref=e6]
- option "Large" [ref=e7]
- option "Small" [selected, ref=e6]
- option "Large" [ref=e7]Dropped shadow root content
Pointing -s at a custom element drops what's in its shadow root:
<main>
<my-card><span>Slotted text</span></my-card>
</main>
<script>
customElements.define('my-card', class extends HTMLElement {
constructor() { super(); this.attachShadow({mode: 'open'}).innerHTML = '<button>Shadow button</button><slot></slot>'; }
});
</script>agent-browser snapshot -s main gives:
- main
- button "Shadow button" [ref=e1]
- StaticText "Slotted text"agent-browser snapshot -s my-card gives:
- StaticText "Slotted text"The button text isn't showing.
What's going on
With -s, take_snapshot collects the DOM ids of everything under the matched element. An AX node counts as inside if its own backendDOMNodeId is in that list. Then any inside node whose parent is not inside becomes a root, and each root gets printed with everything under it.
Some nodes sit between the matched element and its content but fail that check anyway:
- Ignored nodes, like plain wrapper divs. Chrome sends a backendDOMNodeId for these, but build_tree replaces every ignored node with
TreeNode::empty(), which drops the id. In the repro, the two divs around the tablist and the.bardiv are ignored nodes. Chrome sends an id for all three, all three ids are in the list, and all three get dropped. - Shadow DOM nodes, like the select's popup (MenuListPopup) and the slot under it. These keep their ids, but
DOM.describeNodeis called withoutpierce: true, so shadow DOM never ends up in the list.
Either way, the node under the gap looks like it has an outside parent and becomes an extra root. The real root already printed it, because render_tree walks through everything, so it shows up again.
The missing pierce is also why the custom element case drops content. my-card has no role, so it's an ignored node and can't be the root itself. The roots come only from ids in the list, and the shadow button's id isn't there, so nothing leads to it.
Fix
Only pick inside nodes that have no inside ancestor as roots, instead of checking just the parent. That's what top level should mean anyway, and it doesn't matter why a node in the middle failed the check.
Also pass pierce: true to DOM.describeNode, so shadow DOM ends up in the list. collect_backend_node_ids already handles shadowRoots, but they never come back without it.
It's about 15 lines in take_snapshot, plus two e2e tests. PR to follow.
I ran the old and fixed builds side by side:
| Page | Selector | Repeated refs before | After |
|---|---|---|---|
| github.com/vercel-labs/agent-browser | header | 12 | 0 |
| vercel.com | main | 4 | 0 |
| MDN select page | main | 440 | 0 |
On every page the fixed output has the same lines as before, just printed once, so nothing gets dropped.
Source: vercel-labs/agent-browser