Puppeteer: Cannot evaluate a string with arguments
String evaluation is supplied with additional arguments.
String evaluation is supplied with additional arguments.
Error message
Cannot evaluate a string with arguments
How this error happens
evaluateOnNewDocument cannot interpolate arguments into a source-code string. Arguments belong to a function callback.
The snippets use an open Puppeteer page named page.
await page.evaluateOnNewDocument(
"window.theme = theme",
"dark"
);
How to fix
Pass a function and let Puppeteer serialize its arguments.
await page.evaluateOnNewDocument(
(theme) => {
window.theme = theme;
},
"dark"
);
Things to keep in mind
The preload runs in future documents. It does not immediately modify the current page.