html - Selenium:嵌入到 Object 标签中的 SVG 元素未被 Selenium IDE 或 xPath 识别?

标签 html xpath selenium

我有一个这样的 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 路径定位 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="&apos;Dialog&apos;"
 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。

最佳答案

//div[contains(@id='imageholder')/object/svg 失败,因为您没有告诉处理器 svg 元素的 namespace 与另一个不一样。请参阅 xmlns="http://www.w3.org/2000/svg"

你可以这样写://div[contains(@id,'imageholder')]/object/*[local-name()='svg' and namespace-uri()='http://www.w3.org/2000/svg']

编辑(阅读您的更新后):

问题可能是:在您的原始 html 文件中,您的 object 元素中有一个指向 svg 文件的链接。

即使您的浏览器将 svg 文件合并到 dom 中,XPath 表达式似乎也会作用于原始文件本身(带有 href)。抱歉,我不知道 XPath 有那些用例,所以无法告诉您更多信息。

关于html - Selenium:嵌入到 Object 标签中的 SVG 元素未被 Selenium IDE 或 xPath 识别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8398172/

相关文章:

javascript - 在需要访问标题中的文本框的窗口中滚动

html - 在消息字段中隐藏滚动条

xml - XSLT、XPath 和 InStr

python - 使用 Python Selenium 查找 span 标记的兄弟/父级

javascript - Selenium 输入文本不会替换所有值

c# - HtmlAgilityPack SelectNodes 语法

html - 单个导航链接的特定背景

xpath - 在 Xpath 表达式中使用 count

java - 如何找到包含特定文本的第二张图像

angularjs - cucumber / capybara + Angular : Test passes using selenium driver, 但不是恶作剧鬼