1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Vanilla JS getting all links under span using XPath and foreach loop

Discussion in 'JavaScript' started by Stealthrtt, Dec 4, 2021.

  1. #1
    Hey all I can not seem to get this below code to work:

    var links = doc.DocumentNode.SelectSingleNode("/html/body/div[1]/div/div[4]/div/div/div[2]/div[2]/span[1]");
    
        foreach (HtmlNode item in table.SelectNodes("//span[1]/descendant::a[starts-with(@href,'/photo')]"))
        {
            console.log("test");
        }
    Code (JavaScript):
    It keeps telling me:

    It looks like all my ")" are present and in the correct spots?

    the HTML that I am getting the links from looks like this:
    
        <span>
          <a href="/photo356.png" class="_4dvp" id="_D9">
            <div class="_403j">
              <i class="im_2sxw" style="width:100px;height:100px;" aria-label="pic" role="img"></i>
              <div class="_5fef">
                <div class="_5feg" role="link" aria-label="25">
                  <i class="imgMGdk7"></i>25
                </div>
                <div class="fezg" role="link" aria-label="15">
                  <i class="_imgsp_l0"></i>15
                </div>
              </div>
            </div>
          </a>
          <a href="/photo17814561.png" class="_39pvp" id="u3_sz">
            <div class="_403j">
              <i class="imgxw" style="width:100px;height:100px;" aria-label="pic61" role="img"></i>
              <div class="_5f">
                <div class="fzeg" role="link" aria-label="23">
                  <i class="_img2e1b6"></i>23
                </div>
            ...etc....
    HTML:
    the XPath starts at the <span>. The XPath is this:

    But like I said above I just get that JS error. I'm not even sure my XPATH logic is even correct since I'm not able to pass this error and continue on...

    Help would be great!
     
    Stealthrtt, Dec 4, 2021 IP
  2. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #2
    Are you 100% sure that's the section the console is pointing at for the error?

    That said, ouch that non-semantic gibberish markup looks painfully bad. Particularly with the aria label junk that's basically in there because you're "using the wrong markup". NOT that any of that looks like actual LABEL content or IMG tags. Just as I suspect whatever it is you're trying to do with these images should probably be just hooking the proper markup -- the href for the actual image, with a thumbnail inside it instead of all those goofy italic tags... Don't even get me started about the XPATH nonsense that takes something simple -- grabbing some elements -- and overcomplicates it to the sky.

    But that's hard to say without knowing what this is even trying to accomplish. Again though, I'd venture a guess that with that crappy markup it's likely you're over-relying on JavaScript, possibly for nothing.

    Even just the presence of a span around elements containing DIV reeks of not knowing enough HTML to be playing with JavaScript yet.
     
    deathshadow, Dec 15, 2021 IP