2016-11-08 13 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