未捕获(承诺)错误:传递的函数不能很好地序列化

时间:2021-02-20 16:16:07

标签: javascript electron puppeteer

我正在尝试在 Electron 中使用 Puppeteer 来做一些自动化。我使用简单的命令 node test.js 测试了以下代码,没有任何错误:

(async () => {
    const browser = await puppeteer.launch();
    const page = await browser.newPage();
    await page.goto('https://www.facebook.com/');
    await page.waitFor(2000);
    await page.click('button[type="submit"]');
    await page.waitFor(2000);
    await page.screenshot({ path: 'example.png' });
    await browser.close();
  })();

但是当我在 Electron 中运行完全相同的代码时,我收到以下错误,指出 await page.click('button[type="submit"]');

存在错误
Uncaught (in promise) Error: Passed function is not well-serializable!
at ExecutionContext._evaluateInternal (ExecutionContext.ts:247)
at ExecutionContext.evaluateHandle (ExecutionContext.ts:191)
at ElementHandle.evaluateHandle (JSHandle.ts:183)
at Object.internalHandler.queryOne (QueryHandler.ts:68)
at ElementHandle.$ (JSHandle.ts:778)
at DOMWorld.$ (DOMWorld.ts:171)
at async DOMWorld.click (DOMWorld.ts:434)
at async file:/Users/usr/Desktop/aa/renderer.js:17

我该如何解决??

1 个答案:

答案 0 :(得分:0)

尝试传递一个字符串来计算而不是一个函数(用反引号包裹它)。

你也可以尝试用 eval('your-code-as-string');

相关问题