首页 > 解决方案 > 如何使用硒检查svg元素是否存在

问题描述

我有一个 svg 地图,其中包含一些元素,如矩形、圆形、线条、文本等。我需要检查是否存在特定的圆形元素。它在 findElement 方法本身失败,即无法使用代码定位元素。当我检查元素并在 chrome 中输入我的 xpath 时,我得到了该元素,但是当我在 selenium java 代码中使用这个 xpath 时,它无法找到该元素。我尝试了以下方法:

  1. 存在=driver.findElements(by).size() != 0;
  2. 带有 xpath 和 cssSelector 的 findElements 方法

DOM结构:

<circle cx="499.99999999999653" cy="199.00000000000082" r="8" fill="#0000ff" stroke="#ffffff" style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0);"></circle><rect x="459.99999999999653" y="206.00000000000082" width="80" height="10" r="0" rx="0" ry="0" fill="#ffffff" stroke="#000" stroke-width="0" opacity="0.65" style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0); opacity: 0.65;"></rect><text x="499.99999999999653" y="211.00000000000082" text-anchor="middle" font="10px &quot;Arial&quot;" stroke="none" fill="#000000" style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0); text-anchor: middle; font: bold 10px &quot;Trebuchet MS&quot;;" font-weight="bold" font-family="Trebuchet MS"><tspan dy="3.4453125000008242" style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0);">Node 4 Object</tspan></text>

Xpath used : // [name()="svg"]// [name()="text"]// [name()="tspan" and text()="Node 4 Object"]//.. /preceding-sibling:: [位置()=2]

标签: selenium-webdriversvg

解决方案


推荐阅读