Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I locate this element using Xpath?

Tags:

html

xpath

I have the following code:

<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>

I want to locate the element with id="RootMenuItemText" but I need this element to be a child of an element who is following sibling of an element (which id is "RootMenuItem") that has a child with id="RootMenuItemIconHolder" with a "block" value in the "display" attribute.

The element I need to locate is:

<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>

I have tried this Xpath:

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

But it is not locating the element. Does anyone know which Xpath expression will help me to locate the element? Thanks in advance.

like image 521
asmundur Avatar asked Dec 04 '25 10:12

asmundur


1 Answers

Try following:

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

You should also remember, that you have to set proper namespaces to make the selection work. That's because your document uses default xmlns xml namespaces on different levels of the document.

like image 65
MarcinJuraszek Avatar answered Dec 07 '25 00:12

MarcinJuraszek



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!