Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XPath queries in Google Chrome console

I can use XPath queries in Google Chrome's console like that:

a = $x('my/path')

However, what if I want to find an XPath relatively to another object? E.g.:

b = a.$x('my/path')

(does not work), also:

b = $x('my/path', a)

fails with: NotSupportedError: Failed to execute 'evaluate' on 'Document': The context node provided is null.

Does anybody know how to evaluate relative XPaths in Google Chrome's developer console?

like image 395
D.R. Avatar asked Aug 11 '14 07:08

D.R.


People also ask

What are XPath queries?

XPath (XML Path Language) is a query language that can be used to query data from XML documents. In RUEI, XPath queries can be used for content scanning of XML documents. A complete specification of XPath is available at http://www.w3.org/TR/xpath .


1 Answers

Evaluating $x returns ...

function $x(xpath, [startNode]) { [Command Line API] }

So, the syntax is $x('my/path', a).

Important is that, $x returns an array but startNoderequires a DOM node, so you have to take an element of the first query. The following sample demonstrates the behavior on the current page.

a = $x("//*[@id='question-header']")
> [ <div id=​"question-header">​…​</div>​ ]

b = $x(".//*[@href]/text()", a[0])
> [ "XPath queries in Google Chrome console" ]

Update: Here is the documentation.

like image 169
ulrichb Avatar answered Oct 05 '22 21:10

ulrichb