3
votes

I am struggling to target some specific HTML with XPATH...

With CSS, I know the correct targeting is:

html > body > div.page-content > div.container > div.myaccount > div > div:nth-child(4) > ul > [data-coin="BTC"] > div > div:nth-child(2)

The HTML is:

<html lang="en">
   <body style="">
      <div class="page-content">
         <div class="container" style="padding-top:10px;padding-bottom:50px">
            <div class="myaccount">
               <div class="row">
                  <div class="col-md-12">
                     <ul class="listgroup">
                        <li class="hidden-xs hidden-sm list-group-item tradeitem" data-coin="BTC" data-coindisplay="BTC">
                           <div class="row marketrow">
                              <div class="col-sm-2"><img src="/public/img/coinmd/bitcoin.png?v=68" style="height:37px"> &nbsp;Bitcoin</div>
                              <div class="col-sm-2">$23329.28</div>
                              <div class="col-sm-2">$23329.28</div>
                              <div class="col-sm-2">$370,105,221,964</div>
                              <div class="col-sm-1"><span class="moveup">14.76%</span></div>
                              <div class="col-sm-3">
                                 <a href="/buy/btc" class="btn btn-default btn-sm">Buy BTC</a>&nbsp;&nbsp;
                                 <a href="/sell/btc" class="btn btn-default btn-sm">Sell BTC</a>
                              </div>
                           </div>
                        </li>
                     </ul>
                  </div>
               </div>
            </div>
         </div>
      </div>
   </body>
</html>

The XPATH that I've tried is:

//*[contains(concat( " ", @class, " " ), concat( " ", "col-sm-2", " " ))]

^ this works but is too generic, as the HTML element positioning will change everyday. It also doesn't take into account the data attribute of the grandparent HTML.

So my question is what is the XPATH that I need, to specifically target the this element (like I can with CSS)?

<div class="col-sm-2">$23329.28</div>
1
ok i've removed image & uploaded HTML now, can you remove downvote? cheers & thanks - Brett
There is no div:nth-child(4) in this Html. Which tag are you trying to select? - Daniel Manta
@derloopkat - I just updated my question now, with the element I am trying to select (right at the end of my question). thanks - Brett
The first div with $23329.28 is //*[@data-coin='BTC']/div/div[2]. But you don't need SO for this. Just open de page in FF, Opera or Chrome, right click on the element -> inspect -> Copy Xpath, and you get /html/body/div/div/div/div/div/ul/li/div/div[2] - Daniel Manta
thanks @derloopkat! that is very very close. my only problem is that it returns 2 results, because if you look at the HTML, it is duplicated (unfortunately I can't change this). Is there a way to just grab the first of the duplicate? much appreciated - Brett

1 Answers

1
votes

Thanks for those who contributed to this post. My answer I discovered is:
//*[@data-coin='BTC']/div/div[2])[1]