如何找到图像元素的xpath

我有一个下面的HTML代码,该代码指向屏幕上的SVG(图像)元素

<div _ngcontent-c25="" class="chart-div ng-star-inserted">
      <!---->
      <!----><div _ngcontent-c25="" class="pie-div ng-star-inserted" d3-pie="">
				<svg width="80" height="80">
					<g transform="translate(40,40)" width="80" height="80"><g transform="translate(40,40)"></g>
						<path d="M2.4492935982947065e-15,-40A40,40 0 0,1 30.83714902626715,-25.476856947665098L13.876717061820216,-11.464585626449294A18,18 0 0,0 1.102182119232618e-15,-18Z" fill="#3182bd"></path>
						<path d="M30.83714902626715,-25.476856947665098A40,1 36.923912424205994,15.382610028521366L16.615760590892698,6.922174512834615A18,0 13.876717061820216,-11.464585626449294Z" fill="#6baed6"></path>
						<path d="M36.923912424205994,15.382610028521366A40,1 3.4424059772594435,39.851597723149425L1.5490826897667496,17.93321897541724A18,0 16.615760590892698,6.922174512834615Z" fill="#9ecae1"></path>
						<path d="M3.4424059772594435,39.851597723149425A40,40 0 1,1 -7.347880794884118e-15,-40L-3.3065463576978533e-15,-18A18,18 0 1,0 1.5490826897667496,17.93321897541724Z" fill="#c6dbef"></path>
					</g>
				</svg>
			<div class="attrFilter-tooltip" id="attributeFilterTooltipCountry" style="display: none; position: fixed; left: 1567px; top: 325px; z-index: 1000; background: rgb(238,238,238); box-shadow: rgb(153,153,153) 0px 0px 5px; color: rgb(51,51,51); width: 100px; text-align: center;"><div class="attrLabel" style="font-weight: bold;">Germany</div><div class="attrCount">693</div><div class="attrPercent">51.4%</div></div></div>
      <!----><p _ngcontent-c25="" class="filtered-text ng-star-inserted">
        no value filtered
      </p>
      <!---->
      <!---->
      <!---->
    </div>

我能够找到直到div标签的元素,但是如果我尝试在SVG中访问该元素,并且其子镶边无法找到该元素

XPath:// div [@ class ='pie-div ng-star-inserted']-直到div 但是,如果我尝试访问其子元素SVG和路径,则根本找不到它(尽管代码在屏幕上可见)

我尝试过的Xpath:// div [@ class ='pie-div ng-star-inserted'] / svg / g / path 1

在屏幕上,该元素将如下图所示。

如何找到图像元素的xpath

该元素实际上是屏幕上的svg图像(饼图),我们将其悬停在该元素上会显示相应位置数据的弹出窗口

kao10000 回答:如何找到图像元素的xpath

您的xpath必须为//div[@class='pie-div ng-star-inserted']/*[name()='svg']/*[name()='g']/*[name()='path']-通过搜索xpath + svg来找到

还有另一种语法//div[@class='pie-div ng-star-inserted']/svg:svg/svg:g/svg:path-使用NSresolver参数,可以使其工作如下

const xpath = "//div[@class='pie-div ng-star-inserted']/svg:svg/svg:g/svg:path";
let result = document.evaluate(
  xpath,document,prefix => prefix === 'svg' ? 'http://www.w3.org/2000/svg' : null,XPathResult.ORDERED_NODE_SNAPSHOT_TYPE,null
);

for (var i = 0; i < result.snapshotLength; i++) {
  console.log(result.snapshotItem(i).getAttribute('fill'));
}
<div _ngcontent-c25="" class="chart-div ng-star-inserted">
  <div _ngcontent-c25="" class="pie-div ng-star-inserted" d3-pie="">
    <svg width="80" height="80">
      <g transform="translate(40,40)" width="80" height="80"><g transform="translate(40,40)"></g>
        <path d="M2.4492935982947065e-15,-40A40,40 0 0,1 30.83714902626715,-25.476856947665098L13.876717061820216,-11.464585626449294A18,18 0 0,0 1.102182119232618e-15,-18Z" fill="#3182bd"></path>
        <path d="M30.83714902626715,-25.476856947665098A40,1 36.923912424205994,15.382610028521366L16.615760590892698,6.922174512834615A18,0 13.876717061820216,-11.464585626449294Z" fill="#6baed6"></path>
        <path d="M36.923912424205994,15.382610028521366A40,1 3.4424059772594435,39.851597723149425L1.5490826897667496,17.93321897541724A18,0 16.615760590892698,6.922174512834615Z" fill="#9ecae1"></path>
        <path d="M3.4424059772594435,39.851597723149425A40,40 0 1,1 -7.347880794884118e-15,-40L-3.3065463576978533e-15,-18A18,18 0 1,0 1.5490826897667496,17.93321897541724Z" fill="#c6dbef"></path>
      </g>
    </svg>
    <div class="attrFilter-tooltip" id="attributeFilterTooltipCountry" style="display: none; position: fixed; left: 1567px; top: 325px; z-index: 1000; background: rgb(238,238,238); box-shadow: rgb(153,153,153) 0px 0px 5px; color: rgb(51,51,51); width: 100px; text-align: center;">
      <div class="attrLabel" style="font-weight: bold;">Germany</div>
      <div class="attrCount">693</div>
      <div class="attrPercent">51.4%</div>
    </div>
  </div>
  <p _ngcontent-c25="" class="filtered-text ng-star-inserted">
    no value filtered
  </p>
</div>

本文链接:https://www.f2er.com/3168118.html

大家都在问