如何使用XPath查询在SVG图像中通过其href查找图像?

时间:2011-06-17 13:53:41

标签: xpath selenium svg raphael

我的网站包含“div”元素。在这个“div”中,存在一个“svg”元素,它由JavaScript库“RaphaelJS”生成。此外,我已经指示RaphaelJS在SVG元素内部创建一个具有特定href的图像。

为了进行一些GUI测试,我想测试图像是否确实存在于SVG元素内部,具有预期的href。现在我想编写一个XPath查询(如果可能的话,稍后将在Selenium IDE中使用)来进行测试。我似乎无法使用XPather或Selenium IDE完成工作。

以下是该网站的正文简介:

<html lang="en" xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
...
<body>
...
<div id="my-container">
<table id="my-table">
  <tbody>
    <tr>
      <td>
        <div id="my-raphael-canvas">
          <svg xmlns="http://www.w3.org/2000/svg" version="1.1" width="100" height="100">
        <image x="0" y="0" width="10" height="10" preserveAspectRatio="none" href="/images/my-icon.jpg"/>
      </svg>
        </div>
      </td>
    </tr>
  </tbody>
</table>
...
</body>
</html>

如果我想通过“href”查找“image”元素(在svg中),那么XPath查询应该是什么样的?

提前感谢您的帮助!

2 个答案:

答案 0 :(得分:1)

boolean(//xhtml:div[@id = 'my-raphael-canvas']/svg:svg/svg:image[@xlink:href = '/images/my-icon.jpg'])您需要确保前缀xhtml绑定到XHTML名称空间http://www.w3.org/1999/xhtml,前缀为svg到SVG名称空间http://www.w3.org/2000/svg,和前缀xlink到XLink名称空间http://www.w3.org/1999/xlink

实际上,您发布的示例在没有命名空间的情况下具有SVG href元素的image属性,但据我所知,SVG在其xlink:href元素上定义了image属性因此我写的路径就是使用它。

答案 1 :(得分:0)

我解决了使用这个表达式。在我的情况下,在selenium + phpunit

正常工作
//div[@id="my-raphael-canvas"]/*[name()="svg"]/*[name()="image" and position()=1]