2016-05-05 106 views
0

我想在下面的XML代碼中找到rect id值,但將其作爲空列表。任何幫助,非常感謝!!!!!使用Python Selenium Webdriver無法找到SVG元素

<object id="nodia" 
< svg id="svg2" xmlns:svg='http://www.w3.org/2000/svg'> 
    < g id="layer1> 
    <image id="image3022"></image> 
    <rect id="rect8696"></rect> 
    <rect id="rect8996-6" ></rect> 
    </g> 
</svg> 
</object> 

嘗試在兩個鉻所有下面Possibilties和Firefox:

driver.find_element_by_xpath( 「//對象[@ ID = 'nodia']」) - >工作 driver.find_elements_by_xpath(「//[local-name()='svg'和namespace-uri()='http://www.w3.org/2000/svg']「) - >返回空列表 driver.find_elements_by_xpath(」// [local-name()='svg' ]「) - >返回一個 driver.find_elements_by_xpath(」// object [@ id ='nodia']/svg「) - >返回空列表

回答

0

要使用XPath獲取每個rect元素id屬性:

for e in driver.find_elements_by_xpath("id('svg2')//*[name()='rect']") : 
    print e.get_attribute("id") 

或者使用CSS選擇器:

for e in driver.find_elements_by_css_selector("#svg2 rect") : 
    print e.get_attribute("id") 
+0

driver.find_elements_by_xpath(「ID( 'SVG2')// * [名()='rect']「)也只是返回一個空列表而不是rect id值。請幫助! –

相關問題