0

I have a function that contains a nested arrow function. The nested arrow function needs to access the variable attribute which is always undefined. Why is that? How can I make e.getAttribute() access attribute variable?

this.getAttr = async (locator, attribute) => {
        return await page.$eval(locator, e =>
                e.getAttribute(attribute)
        );
};
Saifur
  • 16,081
  • 6
  • 49
  • 73
  • That should work perfectly well as long as `.getAttr()` is passed a value for its second parameter. Have you tried `console.log(attribute)` **before** the `return` statement to verify that it's defined? – Pointy May 03 '21 at 13:33
  • 1
    `page.$eval` suggests you're using some web testing framework. Those will actually *serialise* your function and re-create it on the page. The serialisation process loses all closure references. – VLAZ May 03 '21 at 13:34
  • [how to pass a variable to puppeteer page.$eval();](https://stackoverflow.com/q/59899932) – VLAZ May 03 '21 at 13:35
  • Thanks guys for pointing me to right direction. Here is the working code `this.getAttr = async (locator, attribute) => { return await page.$eval( locator, (element, attribute) => { return element.getAttribute(attribute); }, attribute ); }; ` – Saifur May 03 '21 at 13:53

0 Answers0