How to perform relative XPath queries with Chrome Dev Tools API?

1,142 views
Skip to first unread message

Dan

unread,
Jun 4, 2018, 12:52:18 PM6/4/18
to chrome-debugging-protocol
Hi,

I am interested in performing an XPath Query relative to some node on the DOM using the Devtools API. Basically doing something like ".//div" - using the '.' means that the XPath query is executed relative to the search context from which it originated.

The closest thing I found was DOM.performSearch, however this doesn't seem to provide a mechanism for me to provide a nodeId which is the context from which the XPath expression is executed. Any tips on this?

Andrea Cardaci

unread,
Jun 5, 2018, 12:29:20 PM6/5/18
to chrome-debugging-protocol
You can use Runtime.evaluate to inject a Document.evaluate call.

Daniel Zou

unread,
Jun 5, 2018, 3:33:58 PM6/5/18
to Andrea Cardaci, chrome-debugging-protocol
Thanks for the response. I'm not sure if this quite works for me however because I am using devtool's concept nodeIds; if I understand correctly the Chrome Devtools world maps nodeIds to elements on the DOM. I am afraid that executing raw JS will return me a JS element, but I'm not sure how to convert the element back into a nodeId for me to do further processing on the element with other API calls.

There is also that boolean option includeUserAgentShadowDOM; I'm not entirely sure I understand what that setting is but does it allow me to performSearch on a subset of the DOM? Do you know what is meant by user agent shadow DOM?



--
You received this message because you are subscribed to a topic in the Google Groups "chrome-debugging-protocol" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/chrome-debugging-protocol/XWFeYoBUkds/unsubscribe.
To unsubscribe from this group and all its topics, send an email to chrome-debugging-protocol+unsub...@googlegroups.com.
To post to this group, send email to chrome-debugging-protocol@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/chrome-debugging-protocol/8cd65e93-84d1-4a1e-9b81-46186870ac2d%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Andrea Cardaci

unread,
Jun 5, 2018, 3:46:15 PM6/5/18
to chrome-debugging-protocol
On Tuesday, 5 June 2018 21:33:58 UTC+2, Dan wrote:
Thanks for the response. I'm not sure if this quite works for me however because I am using devtool's concept nodeIds; if I understand correctly the Chrome Devtools world maps nodeIds to elements on the DOM. I am afraid that executing raw JS will return me a JS element, but I'm not sure how to convert the element back into a nodeId for me to do further processing on the element with other API calls.

>>> Runtime.evaluate({expression: `document.body`})
...
{ result: 
   { type: 'object',
     subtype: 'node',
     className: 'HTMLBodyElement',
     description: 'body',
     objectId: '{"injectedScriptId":1,"id":3}' } }
>>> DOM.describeNode({objectId: '{"injectedScriptId":1,"id":3}'})
...
{ node: 
   { nodeId: 0,
     backendNodeId: 4,
     nodeType: 1,
     nodeName: 'BODY',
     localName: 'body',
     nodeValue: '',
     childNodeCount: 1,
     attributes: [] } }
 
There is also that boolean option includeUserAgentShadowDOM; I'm not entirely sure I understand what that setting is but does it allow me to performSearch on a subset of the DOM? Do you know what is meant by user agent shadow DOM?

Sorry not quite.
 


On Tue, Jun 5, 2018 at 12:29 PM, Andrea Cardaci <cyru...@gmail.com> wrote:
You can use Runtime.evaluate to inject a Document.evaluate call.

On Monday, 4 June 2018 18:52:18 UTC+2, Dan wrote:
Hi,

I am interested in performing an XPath Query relative to some node on the DOM using the Devtools API. Basically doing something like ".//div" - using the '.' means that the XPath query is executed relative to the search context from which it originated.

The closest thing I found was DOM.performSearch, however this doesn't seem to provide a mechanism for me to provide a nodeId which is the context from which the XPath expression is executed. Any tips on this?

--
You received this message because you are subscribed to a topic in the Google Groups "chrome-debugging-protocol" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/chrome-debugging-protocol/XWFeYoBUkds/unsubscribe.
To unsubscribe from this group and all its topics, send an email to chrome-debugging-protocol+unsub...@googlegroups.com.
To post to this group, send email to chrome-debug...@googlegroups.com.

Daniel Zou

unread,
Jun 5, 2018, 3:55:34 PM6/5/18
to Andrea Cardaci, chrome-debugging-protocol
Ahh I see, very cool. Thanks for your expertise on the debugging protocol; I appreciate the help!

To unsubscribe from this group and all its topics, send an email to chrome-debugging-protocol+unsubsc...@googlegroups.com.
To post to this group, send email to chrome-debug...@googlegroups.com.

--
You received this message because you are subscribed to a topic in the Google Groups "chrome-debugging-protocol" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/chrome-debugging-protocol/XWFeYoBUkds/unsubscribe.
To unsubscribe from this group and all its topics, send an email to chrome-debugging-protocol+unsub...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages