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

134
Vistas
Puppeteer selector value showing undefined

I'm trying to build code to Web-Scrape my school website and im only in the first trying out stage and im following a tutorial.

const puppeteer = require('puppeteer');

async function scrapeProduct(url) {
    const browser = await puppeteer.launch();
    const page = await browser.newPage();
    await page.goto(url);

    const[el] = await page.$x('//*[@id="post-7577"]/div/div[2]/table[8]/tbody/tr[2]/td[1]');
    const td = await el.getProperty('td');
    const tdTxt = await td.jsonValue();

    console.log({tdTxt});
    console.log({td});

    browser.close();
}

scrapeProduct('https://www.gym-kahla.de/klassen-uebersicht/')

this is the code i've copied from the tutorial and placed my own link and xPath in.

the output in the console after running the file is:

$ node scrapers.js
{ srcTxt: undefined }

which confuses me because whilst the variable ''srcTxt'' was in the tutorial, its not in this changed code anymore is it?

if it helps, here the code (that worked as it did in the video) that i had ran in the same file before:

const puppeteer = require('puppeteer');

async function scrapeProduct(url) {
    const browser = await puppeteer.launch();
    const page = await browser.newPage();
    await page.goto(url);

    const[el] = await page.$x('//*[@id="imgBlkFront"]');
    const src = await el.getProperty('src');
    const srcTxt = await src.jsonValue();

    console.log({srcTxt});
}

scrapeProduct('https://www.amazon.com/Black-Swan-Improbable-Robustness-Fragility/dp/081297381X/ref=sr_1_1?keywords=black+swan+book&qid=1640288170&sprefix=black+swan+%2Caps%2C173&sr=8-1')

next problem, this doesn't work anymore either if i try to run it now.

i honestly have no idea what even to ask so i hoping its such a noob mistake that someone of you all will be able to find out what's going on either way.

ps: this is my first stackoverflow post so please excuse if i wrote to much in the beginning, not the right stuff or didn't respect some etiquette

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

0

Consider using CSS selectors instead of XPath.

const puppeteer = require('puppeteer')

scrapeProduct('https://www.gym-kahla.de/klassen-uebersicht/')

async function scrapeProduct(url) {
  const browser = await puppeteer.launch()
  const page = await browser.newPage()
  await page.goto(url)

  // Parse phone number from phone element
  const elPhone = await page.waitForSelector('.sidebar .textwidget p:nth-child(2)')
  const phoneNumber = await elPhone.evaluate(el => {
    return el.innerText.split(': ')[1]
  })

  // obtain PDF URL of "Kurs 12/2"
  const elLastClassPdf = await page.waitForSelector(
    '#post-7577 .post-content table:last-child tr:last-child td:last-child a'
  )
  const urlPdf = await elLastClassPdf.evaluate(el => {
    return el.getAttribute('href')
  })

  console.log({ phoneNumber, urlPdf })
  
  await browser.close()
}
$ time node scrapers.js
{
  phoneNumber: '036424/52788',
  urlPdf: 'https://www.gym-kahla.de/wp-content/uploads/2021/11/12.pdf'
}

real    0m3.758s
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