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

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

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 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