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

1.7K
Visualizações
Puppeteer clicking link in a span element

I am trying to parse a manga site and save all the pages. I have the code working to run through the page saving the images.

I am having trouble clicking on a link that goes to the next page. I'm working with Node and using Puppeteer. I thought I would be able to just use document.querySelectorAll("span.next-prev-text") to get the span in the link.

Included the relevant code for trying to get the next page and html I am trying to click the NEXT CHAPTER link on.

<div class="col-md-6 prev-post">
  <a class="" href="https://tokyorevengersmanga.com/manga/tokyo-manji-revengers-vol-1-chapter-1-reborn/">
     <span class="next-prev-text">PREVIOUS CHAPTER </span>
  </a>
</div>

<div class="col-md-6 next-post">
  <a class="" href="https://tokyorevengersmanga.com/manga/tokyo-manji-revengers-chapter-3/">
       <span class="next-prev-text">NEXT CHAPTER</span>
  </a>
</div>
(async() => {
    try {
      const browser = await puppeteer.launch();
      const page = await browser.newPage();

      //starting page
      await page.goto(
        "https://tokyorevengersmanga.com/manga/tokyo-manji-revengers-vol-1-chapter-1-reborn/"
      );

      console.log("page has loaded")

      const chapter = await page.evaluate(() => {
        const pages = Array.from(
          document.querySelectorAll("img.aligncenter")
        ).map((image) => image.getAttribute("src"));
        return pages
      });

      fs.writeFileSync("./data.json", JSON.stringify(chapter));
      console.log("File is created!");

      const nextPage = await page.evaluate(() => {
        var obj = document.querySelectorAll("span.next-prev-text");
        return obj[1];
      })

      await page.click(nextPage);
      await page.waitForNavigation();
      await browser.close();
    } catch (error) {
      console.log(error);
    }
over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

The page.click() only takes a string selector. I don't know why but calling page.click(".next-prev-text") throws an error saying Error: Node is either not visible or not an HTMLElement.

The workaround is to use page.$$() method that actually invokes document.querySelectorAll(). It returns an array of ElementHandle so you can simply invoke click() method on the ElementHandle.

I also added the code to handle the popup page when you click the "next chapter" link.

Here is the complete code to test.

const puppeteer = require('puppeteer');
const fs = require('fs');

(async () => {
  try {
    const browser = await puppeteer.launch({
      headless: false
    });
    const page = await browser.newPage();

    //starting page
    await page.goto(
      "https://tokyorevengersmanga.com/manga/tokyo-manji-revengers-vol-1-chapter-1-reborn/"
    );

    console.log("page has loaded")

    const chapter = await page.evaluate(() => {
      const pages = Array.from(
        document.querySelectorAll("img.aligncenter")
      ).map((image) => image.getAttribute("src"));
      return pages
    });

    fs.writeFileSync("./data.json", JSON.stringify(chapter));
    console.log("File is created!");

    const next = await page.$$(".next-prev-text");
    const [popup] = await Promise.all([
      new Promise((resolve) => page.once('popup', async p => {
        await p.waitForNavigation({
          waitUntil: 'networkidle0'
        });
        resolve(p);
      })),
      next[1].click()
    ]);

    // do your job on the next page with 'popup' here

    await browser.close();
  } catch (error) {
    console.log(error);
  }
})();

over 4 years ago · Santiago Trujillo 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