Select tables based on containing text in preceding h3 elements

Issue

  <h3>
    <strong>Tier 1 - 1 USD | 1 EUR</strong>
  </h3>
  <table>
    <thead>
      <tr>
        <th style="text-align:left;">Game
        </th>
        <th style="text-align:center;">Ratings
        </th>
        <th style="text-align:center;">Cards
        </th>
        <th style="text-align:center;">Cheevos
        </th>
        <th style="text-align:center;">Details
        </th>
        <th style="text-align:center;">Platforms
        </th>
        <th style="text-align:center;">Bundled
        </th>
        <th style="text-align:center;">Retail Price
        </th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td style="text-align:left;">
          <strong><a href="https://store.steampowered.com/app/280160/" title="Action, Adventure, Indie, Strategy, Singleplayer, Story Rich, Atmospheric, Multiplayer, Great Soundtrack, Co-op, Open World, Gore, Third Person, Online Co-Op" rel="nofollow noopener" target="_blank">Aragami</a></strong>
        </td>
        <td style="text-align:center;">90% of 7112
        </td>
        <td style="text-align:center;">
          <a href="https://www.steamcardexchange.net/index.php?gamepage-appid-280160" title="9 cards" rel="nofollow noopener" target="_blank">❤</a>
        </td>
        <td style="text-align:center;">
          <a href="https://astats.astats.nl/astats/Steam_Game_Info.php?AppID=280160" title="51 achievements" rel="nofollow noopener" target="_blank">🏆</a>
        </td>
        <td style="text-align:center;">
          <a href="https://www.steamgifts.com/giveaways/search?app=280160">3.00 CV</a>     app/280160
        </td>
        <td style="text-align:center;">W M L
        </td>
        <td style="text-align:center;">
          <a href="https://isthereanydeal.com/specials/#/filter:search/aragami,&amp;bundle" title="1 bundle + 1 special ■ Humble Staff Picks Bundle: Scribble! (3 years ago, tier 2) ■ Platinum Collection - Build Your Own Bundle (Feb / Mar 2021) (5 months ago)" rel="nofollow noopener" target="_blank">1</a>
        </td>
        <td style="text-align:center;">
          <a href="https://isthereanydeal.com/game/aragami/info" title="19.99€" rel="nofollow noopener" target="_blank">$19.99</a>
        </td>
      </tr>
    </tbody>
  </table>
  <h3>
    <strong>Tier 2 - BTA (initial: $6)</strong>
  </h3>
  <table>
    <thead>
      <tr>
        <th style="text-align:left;">Game
        </th>
        <th style="text-align:center;">Ratings
        </th>
        <th style="text-align:center;">Cards
        </th>
        <th style="text-align:center;">Cheevos
        </th>
        <th style="text-align:center;">Details
        </th>
        <th style="text-align:center;">Platforms
        </th>
        <th style="text-align:center;">Bundled
        </th>
        <th style="text-align:center;">Retail Price
        </th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td style="text-align:left;">
          <strong><a href="https://store.steampowered.com/app/551770/" title="Action, Adventure, Indie, Puzzle, Sci-fi, Singleplayer, Story Rich, Atmospheric, Great Soundtrack, Horror, Third Person, Cyberpunk, Stealth, Female Protagonist" rel="nofollow noopener" target="_blank">ECHO</a></strong>
        </td>
        <td style="text-align:center;">80% of 1198
        </td>
        <td style="text-align:center;">-
        </td>
        <td style="text-align:center;">
          <a href="https://astats.astats.nl/astats/Steam_Game_Info.php?AppID=551770" title="47 achievements" rel="nofollow noopener" target="_blank">🏆</a>
        </td>
        <td style="text-align:center;">
          <a href="https://www.steamgifts.com/giveaways/search?app=551770">3.75 CV</a>     app/551770
        </td>
        <td style="text-align:center;">W
        </td>
        <td style="text-align:center;">
          <a href="https://isthereanydeal.com/specials/#/filter:search/echo,&amp;bundle" title="" rel="nofollow noopener" target="_blank">0</a>
        </td>
        <td style="text-align:center;">
          <a href="https://isthereanydeal.com/game/echo/info" title="22.99€" rel="nofollow noopener" target="_blank">$24.99</a>
        </td>
      </tr>
      <tr>
        <td style="text-align:left;">
          <strong><a href="https://store.steampowered.com/app/268130/" title="Action, Adventure, Indie, Strategy, Sci-fi, Singleplayer, Space, Top-Down, Open World, Rogue-like, Tactical, 2D, Rogue-lite, Top-Down Shooter" rel="nofollow noopener" target="_blank">Heat Signature</a></strong>
        </td>
        <td style="text-align:center;">94% of 5571
        </td>
        <td style="text-align:center;">
          <a href="https://www.steamcardexchange.net/index.php?gamepage-appid-268130" title="8 cards" rel="nofollow noopener" target="_blank">❤</a>
        </td>
        <td style="text-align:center;">
          <a href="https://astats.astats.nl/astats/Steam_Game_Info.php?AppID=268130" title="70 achievements" rel="nofollow noopener" target="_blank">🏆</a>
        </td>
        <td style="text-align:center;">
          <a href="https://www.steamgifts.com/giveaways/search?app=268130">2.25 CV</a>     app/268130
        </td>
        <td style="text-align:center;">W
        </td>
        <td style="text-align:center;">
          <a href="https://isthereanydeal.com/specials/#/filter:search/heatsignature,&amp;bundle" title="" rel="nofollow noopener" target="_blank">0</a>
        </td>
        <td style="text-align:center;">
          <a href="https://isthereanydeal.com/game/heatsignature/info" title="12.99€" rel="nofollow noopener" target="_blank">$14.99</a>
        </td>
      </tr>
      <tr>
        <td style="text-align:left;">
          <strong><a href="https://store.steampowered.com/app/355790/" title="Action, Adventure, RPG, Singleplayer, Comedy, Atmospheric, Multiplayer, Co-op, Open World, Violent, Third Person, Difficult, Fantasy, Dark Fantasy" rel="nofollow noopener" target="_blank">Styx: Shards of Darkness</a></strong>
        </td>
        <td style="text-align:center;">83% of 2431
        </td>
        <td style="text-align:center;">
          <a href="https://www.steamcardexchange.net/index.php?gamepage-appid-355790" title="7 cards" rel="nofollow noopener" target="_blank">❤</a>
        </td>
        <td style="text-align:center;">
          <a href="https://astats.astats.nl/astats/Steam_Game_Info.php?AppID=355790" title="38 achievements" rel="nofollow noopener" target="_blank">🏆</a>
        </td>
        <td style="text-align:center;">
          <a href="https://www.steamgifts.com/giveaways/search?app=355790">3.00 CV</a>     app/355790
        </td>
        <td style="text-align:center;">W
        </td>
        <td style="text-align:center;">
          <a href="https://isthereanydeal.com/specials/#/filter:search/styxshardsofdarkness,&amp;bundle" title="2 bundles + 3 specials ■ Kingslayer Bundle (2 years ago) ■ June 2018 Humble Monthly #32 (3 years ago) ■ Platinum Collection - Build Your Own Bundle (Mar 2020) (16 months ago) ⏸ Festival Mystery Bundle (2 years ago) ⏸ Deluxe Mystery Game (2 years ago)" rel="nofollow noopener" target="_blank">2</a>
        </td>
        <td style="text-align:center;">
          <a href="https://isthereanydeal.com/game/styxshardsofdarkness/info" title="19.99€" rel="nofollow noopener" target="_blank">$19.99</a>
        </td>
      </tr>
    </tbody>
  </table>
  <h3>
    <strong>Tier 3 - 12 USD</strong>
  </h3>
  <table>
    <thead>
      <tr>
        <th style="text-align:left;">Game
        </th>
        <th style="text-align:center;">Ratings
        </th>
        <th style="text-align:center;">Cards
        </th>
        <th style="text-align:center;">Cheevos
        </th>
        <th style="text-align:center;">Details
        </th>
        <th style="text-align:center;">Platforms
        </th>
        <th style="text-align:center;">Bundled
        </th>
        <th style="text-align:center;">Retail Price
        </th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td style="text-align:left;">
          <strong><a href="https://store.steampowered.com/sub/272966/" title="" rel="nofollow noopener" target="_blank">HITMAN 2 - Gold Edition</a></strong>
        </td>
        <td style="text-align:center;">No user reviews
        </td>
        <td style="text-align:center;">(Included game has cards)
        </td>
        <td style="text-align:center;">-
        </td>
        <td style="text-align:center;">
          <a href="https://www.steamgifts.com/giveaways/search?q=HITMAN+2+-+Gold+Edition">15.00 CV</a>     sub/272966
        </td>
        <td style="text-align:center;">
        </td>
        <td style="text-align:center;">
          <a href="https://isthereanydeal.com/specials/#/filter:search/hitmaniigoldedition,&amp;bundle" title="0 bundles + 8 specials ⏸ Spring Mystery Bundle (16 months ago) ⏸ Platinum Mystery Bundle (22 months ago) ⏸ Mystery Madness Bundle (22 months ago) ⏸ Staff Picks - Buy More, Save More (2 years ago) ⏸ Elite Mystery Bundle (2 years ago) ⏸ Elite Mystery Bundle (2 years ago) ⏸ Festival Mystery Bundle (2 years ago) ⏸ Deluxe Mystery Game (2 years ago)" rel="nofollow noopener" target="_blank">0*</a>
        </td>
        <td style="text-align:center;">
          <a href="https://isthereanydeal.com/game/hitmaniigoldedition/info" title="89.99€" rel="nofollow noopener" target="_blank">$99.99</a>
        </td>
      </tr>
      <tr>
        <td style="text-align:left;">
          <strong><a href="https://steamdb.info/sub/216806/" title="" rel="nofollow noopener" target="_blank">HITMAN: Game of the Year Edition</a></strong>
        </td>
        <td style="text-align:center;">Corresponds to 
          <a href="https://store.steampowered.com/bundle/4854/" rel="nofollow noopener" target="_blank">this</a> store bundle
        </td>
        <td style="text-align:center;">(Included game has cards)
        </td>
        <td style="text-align:center;">-
        </td>
        <td style="text-align:center;">
          <a href="https://www.steamgifts.com/giveaways/search?q=HITMAN%3A+Game+of+the+Year+Edition">11.09 CV</a>     sub/216806
        </td>
        <td style="text-align:center;">
        </td>
        <td style="text-align:center;">
          <a href="https://isthereanydeal.com/specials/#/filter:search/hitmangameofyearedition,&amp;bundle" title="⏸ Free Game Giveaway 2019-Nov-1 (22 months ago)" rel="nofollow noopener" target="_blank">0*</a>
        </td>
        <td style="text-align:center;">
          <a href="https://isthereanydeal.com/game/hitmangameofyearedition/info" title="71.91€" rel="nofollow noopener" target="_blank">$73.91</a>
        </td>
      </tr>
      <tr>
        <td style="text-align:left;">
          <strong><a href="https://store.steampowered.com/app/417290/" title="Action, Adventure, Indie, RPG, Singleplayer, Action RPG, Story Rich, Atmospheric, Great Soundtrack, Horror, Open World, Exploration, Medieval, Third Person" rel="nofollow noopener" target="_blank">Ghost of a Tale</a></strong>
        </td>
        <td style="text-align:center;">92% of 3029
        </td>
        <td style="text-align:center;">-
        </td>
        <td style="text-align:center;">
          <a href="https://astats.astats.nl/astats/Steam_Game_Info.php?AppID=417290" title="20 achievements" rel="nofollow noopener" target="_blank">🏆</a>
        </td>
        <td style="text-align:center;">
          <a href="https://www.steamgifts.com/giveaways/search?app=417290">3.75 CV</a>     app/417290
        </td>
        <td style="text-align:center;">W
        </td>
        <td style="text-align:center;">
          <a href="https://isthereanydeal.com/specials/#/filter:search/ghostofatale,&amp;bundle" title="0 bundles + 2 specials ⏸ Deluxe Mystery Game (2 years ago) ⏸ Winter Mystery Bundle (2 years ago)" rel="nofollow noopener" target="_blank">0*</a>
        </td>
        <td style="text-align:center;">
          <a href="https://isthereanydeal.com/game/ghostofatale/info" title="22.99€" rel="nofollow noopener" target="_blank">$24.99</a>
        </td>
      </tr>
    </tbody>
  </table>
  <ul>
    <li>10% Humble store coupon for Hitman 3
    </li>
  </ul>
  <h3>
    <strong><a href="https://store.steampowered.com/sub/272966/" rel="nofollow noopener" target="_blank">HITMAN 2 - Gold Edition</a></strong> is delivered as one key that activates the following apps:
  </h3>
  <table>
    <thead>
      <tr>
        <th style="text-align:left;">Game
        </th>
        <th style="text-align:center;">Ratings
        </th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td style="text-align:left;">
          <strong><a href="https://store.steampowered.com/app/863550/" title="Action, Strategy, Puzzle, Singleplayer, Atmospheric, Shooter, Multiplayer, Great Soundtrack, Co-op, Open World, Sandbox, Gore, Violent, Third Person" rel="nofollow noopener" target="_blank">HITMAN™ 2</a></strong>
        </td>
        <td style="text-align:center;">91% of 24178
        </td>
      </tr>
      <tr>
        <td style="text-align:left;">
          <strong><a href="https://store.steampowered.com/app/950556/" title="Action, Gore, Violent, Stealth" rel="nofollow noopener" target="_blank">HITMAN™&nbsp;2 - Hawke's&nbsp;Bay</a></strong> (DLC)
        </td>
        <td style="text-align:center;">92% of 28
        </td>
      </tr>
      <tr>
        <td style="text-align:left;">
          <strong><a href="https://store.steampowered.com/app/950557/" title="Action, Gore, Violent, Stealth" rel="nofollow noopener" target="_blank">HITMAN™&nbsp;2 - Miami</a></strong> (DLC)
        </td>
        <td style="text-align:center;">84% of 52
        </td>
      </tr>
      <tr>
        <td style="text-align:left;">
          <strong><a href="https://store.steampowered.com/app/950558/" title="Action, Gore, Violent" rel="nofollow noopener" target="_blank">HITMAN 2 - Santa Fortuna</a></strong> (DLC)
        </td>
        <td style="text-align:center;">96% of 27
        </td>
      </tr>
      <tr>
        <td style="text-align:left;">
          <strong><a href="https://store.steampowered.com/app/950559/" title="Action, Gore, Violent" rel="nofollow noopener" target="_blank">HITMAN&nbsp;2 - Mumbai</a></strong> (DLC)
        </td>
        <td style="text-align:center;">77% of 27
        </td>
      </tr>
      <tr>
        <td style="text-align:left;">
          <strong><a href="https://store.steampowered.com/app/950560/" title="Action, Gore, Violent" rel="nofollow noopener" target="_blank">HITMAN™&nbsp;2 - Whittleton Creek</a></strong> (DLC)
        </td>
        <td style="text-align:center;">94% of 34
        </td>
      </tr>
      <tr>
        <td style="text-align:left;">
          <strong><a href="https://store.steampowered.com/app/950561/" title="Action, Gore, Violent" rel="nofollow noopener" target="_blank">HITMAN™&nbsp;2 - Isle of Sgàil</a></strong> (DLC)
        </td>
        <td style="text-align:center;">86% of 23
        </td>
      </tr>
      <tr>
        <td style="text-align:left;">
          <strong><a href="https://store.steampowered.com/app/950562/" title="Action, Gore, Violent" rel="nofollow noopener" target="_blank">HITMAN™&nbsp;2 - Himmelstein</a></strong> (DLC)
        </td>
        <td style="text-align:center;">75% of 28
        </td>
      </tr>
      <tr>
        <td style="text-align:left;">
          <strong><a href="https://store.steampowered.com/app/957690/" title="Action, Gore, Violent, Stealth, Assassin" rel="nofollow noopener" target="_blank">HITMAN&nbsp;2 - Expansion Pass</a></strong> (DLC)
        </td>
        <td style="text-align:center;">68% of 388
        </td>
      </tr>
      <tr>
        <td style="text-align:left;">
          <strong><a href="https://store.steampowered.com/app/957691/" title="" rel="nofollow noopener" target="_blank">HITMAN 2 - Expansion Pack 1</a></strong> (DLC)
        </td>
        <td style="text-align:center;">No user reviews
        </td>
      </tr>
      <tr>
        <td style="text-align:left;">
          <strong><a href="https://store.steampowered.com/app/957692/" title="" rel="nofollow noopener" target="_blank">HITMAN 2 - Expansion Pack 2</a></strong> (DLC)
        </td>
        <td style="text-align:center;">No user reviews
        </td>
      </tr>
      <tr>
        <td style="text-align:left;">
          <strong><a href="https://store.steampowered.com/app/957693/" title="" rel="nofollow noopener" target="_blank">HITMAN 2 - Winter Sports Pack</a></strong> (DLC)
        </td>
        <td style="text-align:center;">No user reviews
        </td>
      </tr>
      <tr>
        <td style="text-align:left;">
          <strong><a href="https://store.steampowered.com/app/957694/" title="" rel="nofollow noopener" target="_blank">HITMAN 2 - Smart Casual Pack</a></strong> (DLC)
        </td>
        <td style="text-align:center;">No user reviews
        </td>
      </tr>
      <tr>
        <td style="text-align:left;">
          <strong><a href="https://store.steampowered.com/app/957695/" title="Action" rel="nofollow noopener" target="_blank">HITMAN™ 2 - Special Assignments Pack 1</a></strong> (DLC)
        </td>
        <td style="text-align:center;">100% of 1
        </td>
      </tr>
      <tr>
        <td style="text-align:left;">
          <strong><a href="https://store.steampowered.com/app/957696/" title="" rel="nofollow noopener" target="_blank">HITMAN 2 - Expansion Mission Pack 2</a></strong> (DLC)
        </td>
        <td style="text-align:center;">No user reviews
        </td>
      </tr>
      <tr>
        <td style="text-align:left;">
          <strong><a href="https://store.steampowered.com/app/957697/" title="" rel="nofollow noopener" target="_blank">HITMAN 2 - Executive Pack</a></strong> (DLC)
        </td>
        <td style="text-align:center;">No user reviews
        </td>
      </tr>
      <tr>
        <td style="text-align:left;">
          <strong><a href="https://store.steampowered.com/app/957698/" title="" rel="nofollow noopener" target="_blank">HITMAN 2 - Collector's Pack</a></strong> (DLC)
        </td>
        <td style="text-align:center;">No user reviews
        </td>
      </tr>
      <tr>
        <td style="text-align:left;">
          <strong><a href="https://store.steampowered.com/app/957730/" title="Action" rel="nofollow noopener" target="_blank">HITMAN 2 - New York</a></strong> (DLC)
        </td>
        <td style="text-align:center;">100% of 1
        </td>
      </tr>
      <tr>
        <td style="text-align:left;">
          <strong><a href="https://store.steampowered.com/app/957731/" title="" rel="nofollow noopener" target="_blank">HITMAN 2 - Expansion Map 2</a></strong> (DLC)
        </td>
        <td style="text-align:center;">No user reviews
        </td>
      </tr>
      <tr>
        <td style="text-align:left;">
          <strong><a href="https://store.steampowered.com/app/957733/" title="" rel="nofollow noopener" target="_blank">HITMAN&nbsp;2 - Hantu Port</a></strong> (DLC)
        </td>
        <td style="text-align:center;">No user reviews
        </td>
      </tr>
      <tr>
        <td style="text-align:left;">
          <strong><a href="https://store.steampowered.com/app/957735/" title="" rel="nofollow noopener" target="_blank">HITMAN 2 - Siberia</a></strong> (DLC)
        </td>
        <td style="text-align:center;">No user reviews
        </td>
      </tr>
      <tr>
        <td style="text-align:left;">
          <strong><a href="https://store.steampowered.com/app/972340/" title="" rel="nofollow noopener" target="_blank">HITMAN™ 2 - Gold Edition</a></strong> (DLC)
        </td>
        <td style="text-align:center;">No user reviews
        </td>
      </tr>
      <tr>
        <td style="text-align:left;">
          <strong><a href="https://store.steampowered.com/app/977941/" title="Action, Strategy, Puzzle, Singleplayer, Atmospheric, Shooter, Multiplayer, Great Soundtrack, Co-op, Open World, Sandbox, Gore, Violent, Third Person" rel="nofollow noopener" target="_blank">HITMAN™ 2 - Early Access</a></strong> (DLC)
        </td>
        <td style="text-align:center;">91% of 24087
        </td>
      </tr>
    </tbody>
  </table>

Right, so here are four tables. What I’m trying to achieve is selecting only tables where their preceding element is h3 and where that h3 contains ‘Tier’ or ‘Games’.

I can’t really wrap my head around this at all. I’ve come up with 2 options (one that I’ve been religiously repeating like I’m gonna force it to give me the needed result or something).
The first one is:

//h3[contains (., 'Tier') or (., 'Games')]/following::table/tbody/tr/td[1]

This one seems very straightforward to me, yet it doesn’t get the needed result.
The other one is:

//table[preceding-sibling::h3[contains(., 'Tier') or (., 'Games')]]/tbody/tr/td[1]

This one makes even more sense, yet again it doesn’t get the needed results.

Solution

It’s little tricky but found the solution. If you see below, all the table and h3 tags are siblings. In your xPath you are using table/tbody/tr/td[1] but not specifying which table you are referring.

enter image description here

I used the table index to get elements of particular table. Like following-sibling::table[1]

xPath for Tier1 table: Returns only td tags of Tier1 table.

//strong[contains(text(),'Tier 1 - 1 USD')]//parent::h3//following-sibling::table[1]//tbody//tr//td

xPath for Tier2 table: Returns only td tags of Tier2 table.

//strong[contains(text(),'Tier 2 - BTA')]//parent::h3//following-sibling::table[1]//tbody//tr//td

xPath for Tier3 table: Returns only td tags of Tier3 table.

//strong[contains(text(),'Tier 3 - 12 USD')]//parent::h3//following-sibling::table[1]//tbody//tr//td

Dynamic xPath for all tables:

//strong[contains(text(),' + tableName + ')]//parent::h3//following-sibling::table[1]//tbody//tr//td

Answered By – Nandan A

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published