0

I wrote the below line to get the data using puppeteer, which is working fine:

var testResult = await page.evaluate(() => Array.from(document.getElementsByClassName('abc-class'), e => e.innerText));

But if I make abc-class as a variable MyClassVariable and use it inside this line, it is not working. Any suggestions please?

var MyClassVariable = "abc-class";
var testResult = await page.evaluate(() => Array.from(document.getElementsByClassName(MyClassVariable), e => e.innerText));

The error coming is: Error: Evaluation failed: ReferenceError: MyClassVariable is not defined

Note: the line is part of an async function.

Thompson
  • 1,954
  • 10
  • 33
  • 58
  • What do you mean by it's not working? Is there an error? – Unmitigated Oct 04 '22 at 19:23
  • You have to pass `MyClassVariable` variable to that evaluate function. `var testResult = await page.evaluate(() => Array.from(document.getElementsByClassName(MyClassVariable), e => e.innerText), MyClassVariable);`. – Muhammad Nouman Rafique Oct 04 '22 at 19:57
  • 1
    https://pptr.dev/api/puppeteer.page.evaluate/#example-3 Follow this example, Anything you want to use inside that evaluate function, you have to pass that as additional params to evaluate function and then get that params as arg from the evaluate function. `let MyClassVariable = "abc-class"; const result = await page.evaluate((cls) => { // Get class from evaluate function parameter }, MyClassVariable)` – Muhammad Nouman Rafique Oct 04 '22 at 20:02

0 Answers0