I am using puppeteer and the script works fine when executing directly with node.
However, as soon as I build the source with pkg - page.evaluate and page.waitForFunction functions start failing with the error SyntaxError: Unexpected identifier.
The code in question is
await page.evaluate((el) => {return el.value}, element)
The reason it does not work is because pkg compiles scripts using v8::ScriptCompiler the sources are not kept. So basically (el) => {return el.value} is changed to native code. More details here.
So the solution is to pass a string to page.evaluate instead of a function.
I have been able to achieve this with a not-so-nice looking quirky hack.
page.evaluate(
`(() => {
let els=document.querySelectorAll("${selector}");
let el = els[0];
return el.value;
})();`
);
But this gets quite messy, since I am using xpath selectors as well as css.
What I want to do is something like this
page.evaluate(
`((el) => {
return el.value;
})();`, element
);
But el is undefined here (in browser context). I have been trying to make this work for many hours now. Any help will be greatly appreciated.
Other references
Cannot pkg puppeteer app due to ExecutionContext.js - SyntaxError: Unexpected identifier