Get a node from a11ytree having a node from the DOM tree

64 views
Skip to first unread message

Artem Klementev

unread,
Jul 10, 2024, 6:21:37 PM7/10/24
to chrome-debugging-protocol

I need to find the correspondence between a node in the DOM tree where a click occurred and the same node in the a11yTree. I get the DOM tree node by adding listeners to the page.

document.addEventListener('click', window.handleClick)

I use:

const { nodeId: nodeIdBySelector } = await CDPClient.send('DOM.querySelector', {

    nodeId: root.nodeId,

    selector: '.' + selector

})

to get the nodeId of an element. If I get the a11y tree:

const { nodes: axTree } = await CDPClient.send('Accessibility.getFullAXTree')

and try to find the node with the id obtained above, it won't be there (the id will be different).

Is there a way to find a node in the a11yTree using a node from the DOM tree?

Alex Rudenko

unread,
Jul 12, 2024, 2:23:11 AM7/12/24
to chrome-debugging-protocol
nodeId in the a11y tree result is not the same as nodeId in DOM IIRC. You should match by backendDOMNodeId, which you can obtain in DOM by calling DOM.describeNode with DOM.NodeId. 
Note that using the DOM.NodeId is not recommended as they can be reset by some DOM methods such as DOM.getDocument and exist for very specific DevTools frontend workflows.
Reply all
Reply to author
Forward
0 new messages