Search code examples
seleniumxpathselenium-webdriverlocatexpathnavigator

How to locate element using advance Xpath method?


I have following HTML code snippet:

<div class="pagging-box status-border status-border">
    <div class="left-container">
       <div>
          <div class="clientid-hover">
            <h4>AAB52</h4>
          <div class="hover-title" style="display: none;">
            <p>AAB52</p>
           </div>
           </div>
       <div class="clientid-hover-state pl-info">
          <h4>CO</h4>
       </div>

       <div class="client-name-hover">
          <h4>Daniel-old Polar-old</h4>
       </div>
        <p class="contacting-client">Interview Partially Co..</p>
    </div>
  </div>


   <div class="right-container">
       <div class="start-session pull-right">
         <a href="javascript:void(0)");" class="login-button">
             <i class="fa fa-2x fa-play-circle continue-session"></i>
         </a>                                                  
       </div>
    </div>
</div>

I want to locate the Anchor tag on the basis of <h4>AAB52</h4> text. I have tried to locate the same with siblings like following xpath

//div[h4[contains(. , "AAB56")]]/following-sibling::div[1]

(xpath is not correct as it is locating second one div immediate of AAB52 text )


Solution

  • following-sibling operates on the parent of the context node, so you first need walk up to the correct context node:

    //h4[. = 'AAB52']/../../../following-sibling::div[1]/div/a
    

    Alternatively you can use:

    //div[./div/div/h4 = 'AAB52']/following-sibling::div[1]/div/a