Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Puppeteer gives undefined with $$eval

So, I’m trying to scrape web in order to get their elements link as well as id. However, it throws me an undefined as a result. I know that these classes exist because I use page.evaluate, which gets specified class without hesitation. Cannot figure out why it is not working. Moreover, it looks like, that the console.log is not being printed too

  const flats = await page.$$eval(".result-item-v3", (elements) => {
    console.log(elements.length);
    return elements.forEach((element) => {
      return {
        link: element.querySelector(".object-image-link").getAttribute("href"),
        id: element.attributes.id.value,
      };
    });
  });

  console.log(flats);

This part works like a charm with exact same class:

        const flatsHashId = await newPage.evaluate(() =>
          [...document.querySelectorAll(".result-item-v3")].map(
            (elem) => elem.attributes.id.value
          )
        );

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Just change forEach method to map method. forEach method doesn’t return a new transformed array, instead it returns undefined.

Like this:

const flats = await page.$$eval(".result-item-v3", (elements) => {
  console.log(elements.length);
  return elements.map((element) => {
    return {
      link: element.querySelector(".object-image-link").getAttribute("href"),
      id: element.attributes.id.value,
    };
  });
});

console.log(flats);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading