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

发布于 2024-11-16 02:15:19 字数 981 浏览 2 评论 0 原文

我有一个包含“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 查询应该是什么样子?

预先感谢您的帮助!

I have a website that contains a "div" element. Inside this "div", an "svg" element exists, which is generated by the JavaScript library "RaphaelJS". Furthermore, I have instructed RaphaelJS to create an image with a particular href inside the SVG element.

In order to do some GUI testing, I would like to test whether the image indeed exists inside the SVG element, with the expected href. Now I would like to write an XPath query (to be used later in Selenium IDE if possible) to do the test. I cannot get seem to get the job done using XPather, or Selenium IDE.

Here's how the website's body looks in short:

<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>

How should the XPath query look like, if I want to find the "image" element (inside the svg), by its "href"?

Thanks in advance for your help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

错々过的事 2024-11-23 02:15:19

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 href="http://www.w3.org/1999/xlink" rel="nofollow">http://www.w3.org/1999/xlink

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

boolean(//xhtml:div[@id = 'my-raphael-canvas']/svg:svg/svg:image[@xlink:href = '/images/my-icon.jpg']) where you need to ensure that the prefix xhtml is bound to the XHTML namespace http://www.w3.org/1999/xhtml, the prefix svg to the SVG namespace http://www.w3.org/2000/svg, and the prefix xlink to the XLink namespace http://www.w3.org/1999/xlink.

Actually your posted sample has the href attribute of the SVG image element in no namespace but as far as I know SVG defines an xlink:href attribute on its image element, thus the path I have written uses that.

情未る 2024-11-23 02:15:19

我决定使用这个表达。就我而言,在 selenium + phpunit 上工作正常

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

I resolved using this expression. in my case worked correctly at selenium + phpunit

//div[@id="my-raphael-canvas"]/*[name()="svg"]/*[name()="image" and position()=1]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文