在selenium中捕获svg对象的contentDocument

时间:2016-11-08 09:22:25

标签: c# google-chrome internet-explorer selenium

我正在尝试自动测试在Web应用程序中呈现的svg图像。

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'引发了类型'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的页面?

任何帮助非常感谢。

1 个答案:

答案 0 :(得分:0)

您的代码尝试获取contentDocument <object>的{​​{1}}标记<object>,因此它不应该有效。

<frame>标记可能位于<svg>内,因此请尝试切换到包含_webDriver.SwitchTo().DefaultContent(); _webDriver.switchTo().Frame("YourFrameName"); IWebElement svgElementInsideFrame = _webDriver.FindElement(By.TagName("svg")); 的框架中的内容,然后找到您的元素:

$string = "There four of them. These are: 1 The first one. Its is the most common. 2 The second one. 3 The third one. 4 This is the last.";