如何使用Xpath找到此元素?

时间:2013-09-27 16:02:46

标签: html xpath

我有以下代码:

<g xmlns="http://www.w3.org/2000/svg" id="MainMenu1" display="block" transform="translate(1125, 0)">
   <g dp:id="Content" xmlns:dp="http://dreampark.se/extensions" width="375" height="624" id="MainMenuViewMainMenu_DynamicChild3Content" _shadowId="MainMenuViewMainMenu_DynamicChild3Content">
      <g transform="translate(0,0)" x="0" y="0" clip-path="url(#clipPath59)">
         <g display="block" transform=" translate(0,208)">
            <g id="RootMenuItem" width="376" height="50" display="block" buffered-rendering="static" transform="translate(0,0)">
               <g id="RootMenuItemIconHolder" transform="translate(25,2)" display="none">
                  <image id="RootMenuItemIcon" opacity="1.0" width="40" height="40" display="block"/>
               </g>
               <foreignObject width="260" height="30" x="65" y="10" transform="null">
                  <body xmlns="http://www.w3.org/1999/xhtml">
                     <div xmlns="http://www.w3.org/1999/xhtml" style="font-size:22;color:#AAACAE" id="RootMenuItemText" original_x="null" fill="#AAACAE" font-size="22" width="298">Text 2</div>
                  </body>
               </foreignObject>
            </g>
            <g id="RootMenuItem" width="376" height="50" display="block" buffered-rendering="static" transform="translate(0,52)">
               <g id="RootMenuItemIconHolder" transform="translate(25,2)" display="block">
                  <image id="RootMenuItemIcon" opacity="1.0" width="40" height="40" display="block"/>
               </g>
               <foreignObject width="260" height="30" x="65" y="10" transform="null">
                  <body xmlns="http://www.w3.org/1999/xhtml">
                     <div xmlns="http://www.w3.org/1999/xhtml" style="font-size:22;color:#AAACAE" id="RootMenuItemText" original_x="null" fill="#1D212D" font-size="22" width="298">Text 1</div>
                  </body>
               </foreignObject>
            </g>
         </g>
      </g>
   </g>
</g>

我想找到id="RootMenuItemText"的元素,但是我需要这个元素是一个元素的子元素,它跟随一个元素的兄弟(id"RootMenuItem"id="RootMenuItemIconHolder" "block"属性中"display"值为<div xmlns="http://www.w3.org/1999/xhtml" style="font-size:22;color:#AAACAE" id="RootMenuItemText" original_x="null" fill="#1D212D" font-size="22" width="298">Text 1</div> 的孩子。

我需要找到的元素是:

//*[@id="RootMenuItemIconHolder"][@display='block']/following-sibling::*[last()]

我试过这个Xpath:

{{1}}

但它没有找到元素。有谁知道哪个Xpath表达式将帮助我找到元素?提前谢谢。

1 个答案:

答案 0 :(得分:1)

请尝试以下操作:

//*[@id="RootMenuItemIconHolder"][@display='block']/following-sibling::*[last()]/descendant::div[@id='RootMenuItemText']

您还应该记住,您必须设置适当的命名空间才能使选择工作。那是因为您的文档在文档的不同级别使用默认的xmlns xml名称空间。