title: How to pass a function / variable in Puppeteer page.$eval?
body: <p>I am using Puppeteer to automatically fill a webform. Each time the name in the [input] field should change to a different name from an array.</p>
  <p>If I assign a name directly in the page.$eval function (e.g. &quot;Michael&quot;) everything works as expected.</p>
  <pre><code>   puppeteer.launch({ headless: false }).then(async browser =&gt; {
     const page = await browser.newPage()
     await page.goto('https://example.com/webform')
         
     await page.waitFor('input[name=name]');
     await page.$eval('input[name=name]', (el) =&gt; el.value = &quot;Michael&quot;);
  </code></pre>
  <p>But as soon as I try to pass a variable or function (e.g. name_from_array)</p>
  <pre><code>var name = [&quot;Michael&quot;, &quot;Lisa&quot;, &quot;Jason&quot;]
  const random = Math.floor(Math.random() * name.length);
  var name_from_array = name[random];
  
  puppeteer.launch({ headless: false }).then(async browser =&gt; {
  const page = await browser.newPage()
  await page.goto('https://example.com/webform')
     
  await page.waitFor('input[name=name]');
  await page.$eval('input[name=name]', (el) =&gt; el.value = name_from_array);
  </code></pre>
  <p>I get an error message saying &quot;UnhandledPromiseRejectionWarning: Error: Evaluation failed: ReferenceError: name_from_array is not defined&quot;.</p>
  <p>Any idea how to fix this issue?</p>
  <p>Thanks a lot!</p>