Search code examples
javascriptweb-scrapingevaluatepuppeteer

How can I pass variable into an evaluate function?


I'm trying to pass a variable into a page.evaluate() function in Puppeteer, but when I use the following very simplified example, the variable evalVar is undefined.

I can't find any examples to build on, so I need help passing that variable into the page.evaluate() function so I can use it inside.

const puppeteer = require('puppeteer');

(async() => {

  const browser = await puppeteer.launch({headless: false});
  const page = await browser.newPage();

  const evalVar = 'WHUT??';

  try {

    await page.goto('https://www.google.com.au');
    await page.waitForSelector('#fbar');
    const links = await page.evaluate((evalVar) => {

      console.log('evalVar:', evalVar); // appears undefined

      const urls = [];
      hrefs = document.querySelectorAll('#fbar #fsl a');
      hrefs.forEach(function(el) {
        urls.push(el.href);
      });
      return urls;
    })
    console.log('links:', links);

  } catch (err) {

    console.log('ERR:', err.message);

  } finally {

    // browser.close();

  }

})();

Solution

  • You have to pass the variable as an argument to the pageFunction like this:

    const links = await page.evaluate((evalVar) => {
    
      console.log(evalVar); // 2. should be defined now
      ...
    
    }, evalVar); // 1. pass variable as an argument
    

    You can pass in multiple variables by passing more arguments to page.evaluate():

    await page.evaluate((a, b c) => { console.log(a, b, c) }, a, b, c)
    

    The arguments must either be serializable as JSON or JSHandles of in-browser objects: https://pptr.dev/#?show=api-pageevaluatepagefunction-args