Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Target a nested HTML data attribute with XPATH

Tags:

html

css

xpath

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>
like image 698
Brett Avatar asked Sep 13 '25 08:09

Brett


1 Answers

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

like image 85
Brett Avatar answered Sep 16 '25 01:09

Brett