2011-12-06 49 views
2

我有一個HTML代碼段像這樣Selenium:嵌入Object標籤的SVG元素沒有被Selenium IDE或xPath識別?

<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圖像的元素,但我無法達到它。 您的幫助將不勝感激。 感謝

更多的細節上補充:十二月8,2011]

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但未能成功。 謝謝

有關更多詳細信息,請查找示例代碼。

Sample.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一旦你保存文件。

回答

1

//div[contains(@id='imageholder')/object/svg失敗,因爲您沒有告訴處理器svg元素的名稱空間與另一個元素的名稱空間不同。請參閱xmlns="http://www.w3.org/2000/svg"

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

EDIT(閱讀你的更新後):

問題可以是:在原始的HTML文件,你有你的object元素鏈接到SVG文件。

即使您的瀏覽器在dom中包含svg文件,XPath表達式似乎也會作用於原始文件本身(使用href)。對不起,但我不知道XPath的這些用例,並且不能告訴你更多。

+0

,您的回覆感謝。我嘗試了上面的場景,但它仍然給我noSuchElement異常。 正如我前面提到的,我只能在Chrome瀏覽器中看到svg文件的詳細信息,而不是FF(檢查元素)。是否有可能通過X-path找不到SVG元素,因爲它嵌入在obejct標籤中。?:-( – yami

+0

關於XPath表達式,當您使用'contains'函數時,您是否注意到了這個問題?它不是'''但是','。 –

+0

,文森特,我在問題部分[2011年12月8日]中增加了一些細節。我無法在這裏正確添加代碼。請看看並讓我知道如果可以幫助。如何在這裏插入圖像來顯示你的鉻的檢查元素。在檢查元素上的svg文件之前,我看到一個灰色文本(與svg標籤同名) - ><!DOCTYPE svg PUBLIC「 - // W3C // DTD SVG 1.0 // EN「」http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd"> ...你覺得它會影響它嗎 – yami

0

得到了一些工具來獲取SVG嵌入式文檔。它似乎不可能通過WebElement。但通過JavaScriptExecutor,我們可以獲得完整的DOM參考,並通過標準的DOM模式。 贊:我們可以做 - > docuemnt。的getElementsByTagName( '對象')。contentDocument。這將提供完整的SVG DOM參考。

下面的鏈接似乎不是它的直接答案,但通過這個可以看出Sel2.0的可能性。 Selenium: Can I set any of the attribute value of a WebElement in Selenium?

可能會有所幫助所有:-) 感謝