无法找到SVG元素的Xpath

时间:2019-11-04 11:16:33

标签: selenium svg xpath automation

无法找到SVG元素的xpath。这是SVG的HTML部分

<visual-modern _ngcontent-qqf-c10="" _nghost-qqf-c17="">
<div _ngcontent-qqf-c17="" class="visual visual-card allow-deferred-rendering" focus-nav-mode="Group" initialized="">
<svg width="146.60231660231662" height="65.98455598455598" class="card setFocusRing" tabindex="0" focusable="true" aria-label="Total Sales Reps 171." pbi-focus-tracker-idx="3">
<g transform="translate(73.30115830115831,36.98446549227799)"><text class="value" style="text-anchor: middle; fill: rgb(162, 123, 53); font-family: wf_standard-font, helvetica, arial, sans-serif; font-size: 32px; white-space: pre;">171<title>171</title></text></g><g transform="translate(73.30115830115831,61.00009049227799)"><text class="label" style="fill: rgb(80, 62, 27); font-family: &quot;Segoe UI&quot;, wf_segoe-ui_normal, helvetica, arial, sans-serif; font-size: 12px; text-anchor: middle; white-space: pre;">Total Sales Reps<title>Total Sales Reps</title></text></g><div class="caption" style=""></div></svg></div></visual-modern>

相对和绝对Xpath已经累了。

<visual-modern _ngcontent-qqf-c10="" _nghost-qqf-c17="">
<div _ngcontent-qqf-c17="" class="visual visual-card allow-deferred-rendering" focus-nav-mode="Group" initialized="">
<svg width="146.60231660231662" height="65.98455598455598" class="card setFocusRing" tabindex="0" focusable="true" aria-label="Total Sales Reps 171." pbi-focus-tracker-idx="3">
<g transform="translate(73.30115830115831,36.98446549227799)"><text class="value" style="text-anchor: middle; fill: rgb(162, 123, 53); font-family: wf_standard-font, helvetica, arial, sans-serif; font-size: 32px; white-space: pre;">171<title>171</title></text></g>
<g transform="translate(73.30115830115831,61.00009049227799)"><text class="label" style="fill: rgb(80, 62, 27); font-family: &quot;Segoe UI&quot;, wf_segoe-ui_normal, helvetica, arial, sans-serif; font-size: 12px; text-anchor: middle; white-space: pre;">Total Sales Reps<title>Total Sales Reps</title></text></g><div class="caption" style=""></div></svg></div></visual-modern>

2 个答案:

答案 0 :(得分:0)

您可以尝试选择具有aria-label属性且值为'Total Sales Reps 171.'的svg元素,如下所示:

//svg[@aria-label='Total Sales Reps 171.']

如果aria标签值是唯一的,它应该可以工作。
要积累您的xpath知识,您可以通过一些xpath食谱来查看document

答案 1 :(得分:0)

谢谢大家的帮助。这是解决方案

/ [name()='svg'] / [name()='g'] [1] / [name()='g'] / [1]