0

Using puppeteer in nodejs and trying to extract links from a page.

  let response;
  try {
    response = await page.goto(url)
    const noOfLinks = await page.evaluate(_ => document.querySelectorAll('a[href$=".pgn"]'));
    console.log({ status: response.status(), noOfLinks})
    console.log({href: noOfLinks[0].getAttribute('href')})
  } catch (e) { 
    console.log(e.toString()) 
  }

However, noOfLinks.getAttribute() returns an error: TypeError: noOfLinks[0].getAttribute is not a function

It's obvious why from the previous log statement:

{
  status: 200,
  noOfLinks: {
    '0': {},
    '1': {},...

Each link looks like an empty object. If I run .querySelectorAll and .getAttribute in a browser console for that page, I get the expected return values.

Is there something I need to do differently in puppeteer?

Jeff Lowery
  • 2,492
  • 2
  • 32
  • 40

0 Answers0