0
  • Puppeteer version: 9.0.0
  • Platform / OS version: google cloud functions
  • Node.js version: 14
const puppeteer = require('puppeteer')

exports.MyFunc = function MyFunc(req, res) {

  MyFunc(req, res);
  async function MyFunc(req, res) {

    const browser = await puppeteer.launch();
    const page = await browser.newPage();
    await getSearchResults();
    
     async function getSearchResults() {

      const url = `https://abc.redacted.com/search?q=${query}&f=&orderBy=${sort_by}&skip=0&take=10`;
      console.log(url);
      await page.goto(url, { waitUntil: "domcontentloaded" });
      console.log("Page downloaded"); // It console logs till here

      const getResults =  await page.evaluate(() => {
      let items = [];
      const results = document.querySelectorAll(
        "#mainArea > router-view > ma-serp > div > div.results > div > compose > div > div.results > ma-card"
      );
      console.log(results);

      for (let result of results) {

        console.log(result.querySelector("span")?.innerText ?? "");

        items.push({ title: result.querySelector("span")?.innerText ?? "", })
      };
      return items;
      });

      const data = getResults;
      res.status(200).json(data);  // just getting {}

      await browser.close();
      }
  }
}

IDK why but page.evaluate() doesn't console log anything and doesn't return anything to node environment. From three days I'm trying different solutions on stack overflow and GitHub issues but no success until now.

  • I've also tried promise.resolve() when returning from page.evaluate but that doesn't work either.
a.ak
  • 659
  • 2
  • 12
  • 26
  • It looks like you have an HTTP function and if so, you need to make sure it follows the [Express notation](https://firebase.google.com/docs/functions/http-events). Also, you can't create a function inside a function, they have to be created separately. After fixing this, this function may work. – Farid Shumbar Apr 30 '21 at 13:48

1 Answers1

0

When you run page.evaluate() you are actually operating within the browser context, not Node. So console.log outputs to the browser context and you will not see anything in your Node console.

Here is a workaround to get the browser context to output to the node console:

const page = await browser.newPage();

page.on('console', consoleObj => console.log(consoleObj.text()));

Reference: Puppeteer log inside page.evaluate

Benny
  • 156
  • 9