我可以像这样在 Google Chrome 的控制台中使用 XPath 查询:
a = $x('my/path')
但是,如果我想找到相对于另一个对象的 XPath 怎么办?例如。:
b = a.$x('my/path')
(不起作用),还有:
b = $x('my/path', a)
失败:
NotSupportedError: Failed to execute 'evaluate' on 'Document': The context node provided is null.
有人知道如何在 Google Chrome 的开发者控制台中评估相对 XPaths 吗?
最佳答案
正在评估 $x
返回...
function $x(xpath, [startNode]) { [Command Line API] }
所以,语法是
$x('my/path', a)
.重要的是,
$x
返回一个数组,但 startNode
需要一个 DOM 节点,因此您必须获取第一个查询的元素。以下示例演示了当前页面上的行为。a = $x("//*[@id='question-header']")
> [ <div id="question-header">…</div> ]
b = $x(".//*[@href]/text()", a[0])
> [ "XPath queries in Google Chrome console" ]
更新:这是documentation .
关于google-chrome - Google Chrome 控制台中的 XPath 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25237790/