首页 > 解决方案 > 未捕获(承诺中)错误:传递的函数不能很好地序列化

问题描述

我正在尝试在 Electron 中使用 Puppppeteer 进行一些自动化。我用简单的命令测试了以下代码,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

我该如何解决?

标签: javascriptelectronpuppeteer

解决方案


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

您也可以尝试用 eval('your-code-as-string'); 包装它。


推荐阅读