【问题标题】:Uncaught (in promise) Error: Passed function is not well-serializable未捕获(承诺中)错误:传递的函数不能很好地序列化
【发布时间】:2021-02-21 03:25:45
【问题描述】:

我正在尝试在 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

我该如何解决??

【问题讨论】:

    标签: javascript electron puppeteer


    【解决方案1】:

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

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

    【讨论】:

    • 嗨,感谢您的回答。在我将代码更改为 await page.evaluate(`page.click('[type="submit"]')`); 后,它给了我一个新错误“评估失败:ReferenceError:页面未定义”,有什么想法吗?
    猜你喜欢
    • 1970-01-01
    • 2019-01-08
    • 1970-01-01
    • 1970-01-01
    • 2018-11-05
    • 2021-04-11
    • 1970-01-01
    • 2018-07-04
    • 1970-01-01
    相关资源
    最近更新 更多