【发布时间】:2014-06-26 17:47:27
【问题描述】:
我正在尝试使用 XPath 选择器通过 nightwatch.js 命令来定位 Polymer 自定义元素的子元素。例如:
browser
.getAttribute('//th-compare-chart/svg', 'width', function(svgWidth) {
...
})
产量:
ERROR: Unable to locate element: "//th-compare-balance-chart/svg" using: css selector
getAttribute 和其他命令的文档指出,您可以提供 css 选择器或 xpath 的字符串选择器: http://nightwatchjs.org/api#getAttribute
但我还没有让任何 xpath 选择器工作。有谁知道如何让它工作?
### 更新这是 XML 标记,使用 HTML 5 文档类型。该元素是使用聚合物构建的,根据检查员的说法,它嵌套在一个奇怪的“#shadow-root”中。也许无法访问 shadow-root 中的元素?
<th-compare-balance-chart class="element ">
#shadow-root
<core-style>...</core-style>
<svg id="chart">...</svg>
#shadow-root
</th-compare-balance-chart>
【问题讨论】:
-
您确定 SVG 标签中没有
xmlns="http://www.w3.org/2000/svg"吗?除非以某种方式对该文档进行了预处理并且#shadow-root被XML 元素替换,否则XPath 处理器将简单地将#shadow-root视为<svg>的兄弟文本节点。您也可以尝试//th-compare-balance-chart//svg,它会跳过svg标记之前的任何内容,或者//th-compare-balance-chart//*[local-name()='svg'],以防也以某种方式插入命名空间声明。 -
呃,这些路径都不起作用。 th-compare-balance-chart 及其子项被动态添加到页面中,因此它不在页面源中。但我正在查看 chrome 中的元素检查器,我看到 svg 元素上的其他属性,但没有 xmlns 属性。我开始认为这与 Polymer 渲染事物的方式有关。
-
我认为它与聚合物渲染有关的另一个原因是因为使用 getAttribute(
, , callback(result)) 结果.value 为空。 -
试试看你用
//th-compare-balance-chart/*和//th-compare-balance-chart//*得到了什么 -
无法定位元素:“//th-compare-balance-chart//*”使用:xpath 和无法定位元素:“//th-compare-balance-chart/*”使用: xpath... 我真的可以访问那些元素吗!这很蹩脚。
标签: node.js selenium xpath polymer nightwatch.js