Search code examples
node.jses6-promisejestjspuppeteer

puppeteer evaluate results in context destroyed


Trying to wait for DOM mutations to stop but ends with Execution context was destroyed., any suggestion is welcome

            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 error (Runtime.callFunctionOn): Execution context was destroyed. undefined

  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)

Solution

  • The above snippet was run before getting a navigating lock on a page. Running the page.evaluate between navigation can throw this error.

    Found this from,

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

    Fix was (at least in my case) to wait till URL changed and then page.evaluate.