如何使用 XPath 查询在 SVG 图像内通过其 href 查找图像?
我有一个包含“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 查询应该是什么样子?
预先感谢您的帮助!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
boolean(//xhtml:div[@id = 'my-raphael-canvas']/svg:svg/svg:image[@xlink:href = '/images/my-icon.jpg']) ,您需要确保前缀
href="http://www.w3.org/1999/xlink" rel="nofollow">http://www.w3.org/1999/xlink。xhtml
绑定到 XHTML 命名空间 http://www.w3.org/1999/xhtml,SVG 命名空间的前缀svg
http://www.w3.org/2000/svg,以及 XLink 命名空间 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 prefixxhtml
is bound to the XHTML namespace http://www.w3.org/1999/xhtml, the prefixsvg
to the SVG namespace http://www.w3.org/2000/svg, and the prefixxlink
to the XLink namespace http://www.w3.org/1999/xlink.Actually your posted sample has the
href
attribute of the SVGimage
element in no namespace but as far as I know SVG defines anxlink:href
attribute on itsimage
element, thus the path I have written uses that.我决定使用这个表达。就我而言,在 selenium + phpunit 上工作正常
I resolved using this expression. in my case worked correctly at selenium + phpunit