Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

133
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!