Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

182
Vistas
Failed to scrape the link to the next page using xpath in puppeteer

I'm trying to scrape the link to the next page from this webpage. I know how to scrape that using css selector. However, things go wrong when I attempt to parse the same using xpath. This is what I get instead of the next page link.

const puppeteer = require("puppeteer");
let url = "https://stackoverflow.com/questions/tagged/web-scraping";
 
(async () => {
    const browser = await puppeteer.launch({headless:false});
    const [page] = await browser.pages();
    
    await page.goto(url,{waitUntil: 'networkidle2'});
    let nextPageLink = await page.$x("//a[@rel='next']", item => item.getAttribute("href"));
    // let nextPageLink = await page.$eval("a[rel='next']", elm => elm.href);
    console.log("next page:",nextPageLink);
    await browser.close();
})();

How can I scrape the link to the next page using xpath?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

  1. page.$x(expression) returns an array of element handles. You need either destructuring or index acces to get the first element from the array.
  2. To get a DOM element property from this element handle, you need either evaluating with element handle parameter or element handle API.
const [nextPageLink] = await page.$x("//a[@rel='next']");
const nextPageURL = await nextPageLink.evaluate(link => link.href);

Or:

const [nextPageLink] = await page.$x("//a[@rel='next']");
const nextPageURL = await (await nextPageURL.getProperty('href')).jsonValue();
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda