Search code examples
htmlxmlxpathgoogle-sheets-formula

How to get value inside some div below a "::before" using the full Xpath?


I would like to retrieve a data inside several <div> and those are between a ::before and ::after.

I'm using the full xpath because I'm retrieving this data from google sheet using the IMPORTXML function

Here are the two xpaths I've used, neither of which works (Using both in the google chrome console, they do point me to the right element)

/html/body/div[4]/div[4]/div[3]/main/div[3]/div[2]/div[1]/div[2]/div/table/tbody/tr/td[1]/a/img//@data-src
/html/body/div[4]/div[4]/div[3]/main/div[3]/div[2]/div[1]/div[@class='table-wide']/div/table/tbody/tr/td[1]/a/img//@data-src

[EDIT] And to check the HTML code, you can go on this page : https://honkai-star-rail.fandom.com/wiki/Light_Cone/List and inspect the first image of the table.

<!DOCTYPE html>
<html class="client-js windows desktop dyiffygjh idc0_347 ve-available portrait" lang="en" dir="ltr">
<head>
  <title></title>
</head>
<body class="mediawiki ltr sitedir-ltr mw-hide-empty-elt ns-0 ns-subject page-Light_Cone_List rootpage-Light_Cone skin-fandomdesktop action-view ooui-theme-fandomooui wiki-houkaistarrail theme-fandomdesktop-dark no-featured-video wikia-bar-visible menu-bright page-dark">
  <div class="main-container">
    <div id="age-gate"></div>
    <div class="resizable-container">
      <div class="page has-right-rail">
        <main class="page__main" lang="en">
          <div id="content" class="page-content ve-init-mw-desktopArticleTarget-targetContainer">
            <div id="mw-content-text" class="mw-body-content mw-content-ltr" lang="en" dir="ltr">
              <div class="mw-parser-output">
                <div class="table-wide">
                  ::before
                  <div class="table-wide-inner">
                    <table class="article-table sortable jquery-tablesorter" style="width: content-min">
                      <thead>
                        <tr>
                          <th class="headerSort" tabindex="0" role="columnheader button" title="Sort ascending">Icon & Name</th>
                          <th class="mw-collapsible headerSort mw-made-collapsible" id="mw-customcollapsible-lightconetablerarity" tabindex="0" role="columnheader button" title="Sort ascending">Rarity</th>
                          <th class="mw-collapsible headerSort mw-made-collapsible" id="mw-customcollapsible-lightconetablepath" tabindex="0" role="columnheader button" title="Sort ascending">Path</th>
                          <th class="mw-collapsible headerSort mw-made-collapsible" id="mw-customcollapsible-lightconetablestats" tabindex="0" role="columnheader button" title="Sort ascending">Stats</th>
                          <th class="mw-collapsible headerSort mw-made-collapsible" id="mw-customcollapsible-lightconetable" tabindex="0" role="columnheader button" title="Sort ascending">Passive</th>
                          <th class="mw-collapsible headerSort mw-made-collapsible" id="mw-customcollapsible-lightconetablecharacters" tabindex="0" role="columnheader button" title="Sort ascending">Featured Characters</th>
                        </tr>
                      </thead>
                      <tbody>
                        <tr>
                          <td>
                            <a href="/wiki/A_Secret_Vow" title="A Secret Vow"><img alt="Light Cone A Secret Vow" src="https://static.wikia.nocookie.net/houkai-star-rail/images/a/a2/Light_Cone_A_Secret_Vow.png/revision/latest/scale-to-width-down/50?cb=20230719025735" decoding="async" loading="lazy" width="50" height="59" data-image-name="Light Cone A Secret Vow.png" data-image-key="Light_Cone_A_Secret_Vow.png" data-src="https://static.wikia.nocookie.net/houkai-star-rail/images/a/a2/Light_Cone_A_Secret_Vow.png/revision/latest/scale-to-width-down/50?cb=20230719025735" class="ls-is-cached lazyloaded"></a><br>
                            <a href="/wiki/A_Secret_Vow" title="A Secret Vow">A Secret Vow</a>
                          </td>
                          <td class="mw-collapsible mw-made-collapsible" id="mw-customcollapsible-lightconetablepath"><span style="white-space:nowrap;"><a href="/wiki/The_Destruction" title="The Destruction"><img alt="Path The Destruction" src="https://static.wikia.nocookie.net/houkai-star-rail/images/3/37/Path_The_Destruction.png/revision/latest/scale-to-width-down/20?cb=20230508230545" decoding="async" loading="lazy" width="20" height="20" data-image-name="Path The Destruction.png" data-image-key="Path_The_Destruction.png" data-src="https://static.wikia.nocookie.net/houkai-star-rail/images/3/37/Path_The_Destruction.png/revision/latest/scale-to-width-down/20?cb=20230508230545" class="ls-is-cached lazyloaded"></a> <a href="/wiki/The_Destruction" title="The Destruction">The Destruction</a></span></td>
                          <td class="mw-collapsible mw-made-collapsible" id="mw-customcollapsible-lightconetablestats"><b>HP:</b>&nbsp;48~1,058<br>
                          <b>ATK:</b>&nbsp;21~476<br>
                          <b>DEF:</b>&nbsp;12~264</td>
                          <td class="mw-collapsible mw-made-collapsible" id="mw-customcollapsible-lightconetable"><b>Spare No Effort</b><br>
                          Increases DMG dealt by the wearer by <b>20</b>~<b>40</b>%. The wearer also deals an extra <b>20</b>~<b>40</b>% of DMG to enemies with a higher HP percentage than the wearer.</td>
                          <td class="mw-collapsible mw-made-collapsible" id="mw-customcollapsible-lightconetablecharacters">
                            <ul>
                              <li>
                                <a href="/wiki/Arlan" title="Arlan">Arlan</a>
                              </li>
                            </ul>
                          </td>
                        </tr>
                      </tbody>
                    </table>
                  </div>
                </div>
              </div>
              ::after
            </div>
          </div>
        </main>
      </div>
    </div>
  </div>
</body>
</html>

I appreciate all the support ! Thanks !


Solution

  • I believe your goal is as follows.

    • You want to retrieve the following value from the URL of https://honkai-star-rail.fandom.com/wiki/Light_Cone/List using IMPORTXML.

      https://static.wikia.nocookie.net/houkai-star-rail/images/a/a2/Light_Cone_A_Secret_Vow.png/revision/latest/scale-to-width-down/50?cb=20230719025735
      

    In this case, how about the following XPath?

    //tr[2]/td[1]//@data-src
    

    And, how about the following sample formula?

    =IMPORTXML(A1,"//tr[2]/td[1]//@data-src")
    
    • In this case, https://honkai-star-rail.fandom.com/wiki/Light_Cone/List is put into cell "A1".

    When this sample formula is used, the following result is obtained. In this test, the cell "A3" has a formula of =IMAGE(IMPORTXML(A1,"//tr[2]/td[1]//@data-src")).

    enter image description here