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

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");
    }

It keeps telling me:

Uncaught SyntaxError: missing ) after argument list

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....

the XPath starts at the . The XPath is this:

/html/body/div[1]/div/div[4]/div/div/div[2]/div[2]/span[1]

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!

Your syntax of the foreach is completely wrong.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.