Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

191
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda