node.js - puppeteer 在上下文中评估结果被破坏

标签 node.js es6-promise jestjs puppeteer

尝试等待 DOM 突变停止,但以执行上下文被破坏结束。,欢迎任何建议

            page.evaluate(() => {

                return new Promise((resolve,reject) => {

                    var timerId;

                    function resetTimer() {
                        clearInterval(timerId);
                        timerId = setTimeout(() => {
                            resolve(true);
                        }, 3000)
                    }

                    new MutationObserver(
                        () => {
                            resetTimer();
                        }
                    ).observe(document.getElementById('root'), {
                        attributes: true, childList: true
                    });

                    resetTimer();

                })
            })
        })

协议(protocol)错误 (Runtime.callFunctionOn):执行上下文已被破坏。未定义

  at Promise (node_modules/puppeteer/lib/Connection.js:198:56)
  at CDPSession.send (node_modules/puppeteer/lib/Connection.js:197:12)
  at ExecutionContext.evaluateHandle (node_modules/puppeteer/lib/ExecutionContext.js:71:75)
  at ExecutionContext.evaluate (node_modules/puppeteer/lib/ExecutionContext.js:46:31)
  at Frame.evaluate (node_modules/puppeteer/lib/FrameManager.js:299:20)

最佳答案

上面的代码片段是在页面上获得导航锁之前运行的。在导航之间运行 page.evaluate 可能会引发此错误。

发现此,

  1. Error: Protocol error (Runtime.callFunctionOn): Execution context was destroyed.
  2. No page navigation lock ?

修复方法是(至少在我的情况下)等待 URL 更改,然后 page.evaluate

关于node.js - puppeteer 在上下文中评估结果被破坏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49806081/

相关文章:

javascript - 使用 Node.JS 可以同时运行的 setTimeout() 方法的最大数量是多少

javascript - 如何将异步函数映射到列表上?

reactjs - 如何用 enzyme 的mount方法测试React组件

reactjs - "npm test"虽然测试成功但失败(使用 create-react-app 创建的 reactjs 应用程序)

node.js - 将 websockets 与标准 http.Server 集成

javascript - Mongoose:查询年份列表

Javascript 如何手动启动 Star Promise Chain 执行

javascript - 为 promise 链导出 Express 路由方法的最佳方式?

testing - 开 Jest beforeEach/afterEach 只是这个范围内的 block ?

ruby-on-rails - 是否可以在 Rails 6 中禁用 Sass?