Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

182
Views
how can I select all anchor tag href URL that is nested inside a tr inside tbody of a table with javascript

codders.

I want to do some tasks with a puppeteer.

I want to log in to a page and get the URL of the first ten articles listed on that page.

but these urls are nested inside a table. I need to grab the first 10 or even twenty URLs of that page.

here is the structure of the website the

Html body tag has a div tag with #body, inside this div are the 3 tables, the second table is the 9th child inside the div with #body.

--there are 3 Html tables on the page, but the URLs I want to scrape are inside the second table

here is what the second table look like

<table>
<tbody>

<tr> <th>Followed Topics</th> </tr>  

<tr>

<td id="top6803855" class="w"> 

<a name="6803855"></a>
<img src="/icons/normal_post.gif"> 
<b> <a href="/politics">Politics</a> </b> 
    " / "
<b><a href="/6803855/nnamdi-kanu-ifeanyi-ubah-wants">The main article which i want to grab the href</a></b>
    &nbsp;
<a onclick="unfollowtopic('anylink.com', '6803855'); return false;" href="anylink.com">

<img src="/static/delete.png"></a>
<br>

<span class="s">
by <b><a href="/username">username</a></b>.
<b>19</b> posts &amp; <b>389</b> views. <b>12:31am</b> 
(<b><a href="/username">username</a></b>)
</span>

</td>

</tr>

</tbody>

</table>

this is what i have used so far and it work without any issue on localhost.

document.querySelector("body > div > table:nth-child(9) > tbody > tr:nth-child(2) > td > b:nth-child(4) > a").href

i repeat the above selector by changing the no in the :nth-child() so as to grab the remaining tr

but its not working well on Heroku, sometime it select the element, sometime it display error

'Cannot read properties of null (reading 'href')'

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Maybe try to go for last child "b" of the span class:

document.querySelector("body > div > table:nth-child(9) > tbody > tr:nth-child(2) > span.s > b:last > a").href

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!