【发布时间】:2012-01-13 22:51:25
【问题描述】:
我有一个这样的 html 段
<div id="imageholder>
<object data="1.svg" width="100%" height="100%" type="image/svg+xml">
</object>
</div>.
当我在 Firefox/firebug 中看到检查元素时,我看不到 SVG 文件结构, 但在 Chrome 中,我可以看到 svg 文件结构,如下所示:
<div id="imageholder>
<object data="1.svg" width="100%" height="100%" type="image/svg+xml">
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" stroke-dasharray="none"
shape-rendering="auto" font-family="'Dialog'" width="4800">
<desc>widthmm=90.48768097536195 heightmm=49.38127063754128</desc>
<g>
<g>....</g>
</g>
但是,我无法通过 X-path 找到 svg 标签或任何 g 元素。我试过了
//div[contains(@id='imageholder')]
然后我正确地得到了元素。
//div[contains(@id='imageholder')/object]
也正确返回元素。但是
//div[contains(@id='imageholder')/object/svg]
失败。
我的要求是:我需要点击 SVG 图像的几个 g 元素,但我无法达到它。 您的帮助将不胜感激。 谢谢
添加了更多详细信息:[2011 年 12 月 8 日]
driver.findElement(By.xpath("//div[@id='imageholder']/object"));//This worked correctly
driver.findElement(By.xpath("//div[@id='imageholder']/object/*[local-name()='svg' and namespace-uri()='http://www.w3.org/2000/svg']"));//did not work
我还尝试了此链接上给出的所有可能的组合XPath - Find elements by attribute namespace,但未能成功。 谢谢
更多详情,请查看示例代码。
示例.html
<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE9;chrome=IE8">
<body>
<div id="imageholder"><object data="1.svg" width="100%" height="100%" type="image/svg+xml"></object></div>
</body>
</html>
1.svg
<?xml version="1.0"?>
<!DOCTYPE svg PUBLIC '-//W3C//DTD SVG 1.0//EN'
'http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd'>
<svg stroke-dasharray="none" shape-rendering="auto" xmlns="http://www.w3.org/2000/svg" font-family="'Dialog'"
width="4800" text-rendering="auto" fill-opacity="1" contentScriptType="text/ecmascript" color-interpolation="auto"
color-rendering="auto" preserveAspectRatio="xMidYMid meet" font-size="12" viewBox="0 0 4800 2619" fill="black"
xmlns:xlink="http://www.w3.org/1999/xlink" stroke="black" image-rendering="auto" stroke-miterlimit="10"
zoomAndPan="magnify" version="1.0" stroke-linecap="square" stroke-linejoin="miter" contentStyleType="text/css"
font-style="normal" height="2619" stroke-width="1" stroke-dashoffset="0" font-weight="normal" stroke-opacity="1"
>
<g style="fill-opacity:0.7; stroke:black; stroke-width:0.1cm;">
<circle cx="6cm" cy="2cm" r="100" style="fill:red;"
transform="translate(0,50)"/>
<circle cx="6cm" cy="2cm" r="100" style="fill:blue;"
transform="translate(70,150)"/>
<circle cx="6cm" cy="2cm" r="100" style="fill:green;"
transform="translate(-70,150)"/>
</g>
</svg>
保存文件后请点击 sample.html。
【问题讨论】: