2016-11-08 12 views
1

我试图自动化测试在web应用程序中呈现的svg图像。在硒中为svg对象捕获contentDocument

的HTML看起来喜欢 -

<object class="svgcanvas" type="image/svg+xml" data="test.svg"><h4>Error  loading SVG file</h4> 
    #document 
    <svg xmlns="http://www.w3.org/2000/svg"  xmlns:xlink="http://www.w3.org/1999/xlink" -webkit-user-drag: none; -webkit-tap- highlight-color: rgba(0, 0, 0, 0);" viewBox="0 0 1188 840" xml:space="preserve"> 
     <rect x="0" y="0" width="1188" height="840" style="stroke:none;  fill:white"/> 
     <circle cx="20" cy="830" r="0.144857" style="stroke:#E50033; stroke- width:0.339429"/> 
     <g id="XMP_1" style="stroke:#000000; stroke-width:0.339429"> 
     <path d="M554 401L641 401" style="stroke:#0000FF; stroke-dasharray: 3.5640 3.5640 3.5640 3.5640"/> 
     </g> 
</object> 

我尝试使用下面的代码来获取SVG对象,但svgElement引发以下错误:

'((OpenQA.Selenium.Remote.RemoteWebElement)svgElement).Displayed' threw an exception of type 'OpenQA.Selenium.StaleElementReferenceException'

IWebElement objectTag = _webDriver.FindElement(By.CssSelector("object.svgcanvas")); 
IJavaScriptExecutor js = (IJavaScriptExecutor)_webDriver; 
string findingSVG = "return arguments[0].contentDocument;"; 
var svgElement = js.ExecuteScript(findingSVG, objectTag); 

那么,有没有一种方法测试在对象标签中嵌入了SVG的页面?

任何帮助非常感谢。

+0

陈旧的元素通常意味着你发现了元素之后和之前调用对该页面重新加载/更改的任何操作。确保页面已加载并完成所有请求。 – lauda

+0

在开始尝试操纵SVG之前,需要一种方法来确保SVG和主DOM已加载。 Google(或StartPage):selenium等待页面加载javascript – MikeJRamsey56

+0

是否在'isDisplayed()'行引发异常?帧内是否有''? – Moshisho

回答

0

你的代码试图获得contentDocument<object>标签returns a Document object from a frame,所以它不应该工作。

<object>标签可能是一个<frame>内部,尝试切换到该框架包含<svg>,然后找到你的元素内容:

_webDriver.SwitchTo().DefaultContent(); 
_webDriver.switchTo().Frame("YourFrameName"); 

IWebElement svgElementInsideFrame = _webDriver.FindElement(By.TagName("svg")); 
+0

使用Iframe我能够完成它。但是这里的问题是Iframe没有被用来渲染嵌入svg内容的对象标签。在javscript中,我能够得到objectDocument的对象标签,但是当我执行相同的脚本使用硒它不工作。 – Mary

+0

您是否有示例网址?关于什么selenium命令是抛出的异常? – Moshisho

+0

对不起,我没有任何示例url。但我上面提到的例外。 – Mary