尝试等待 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
可能会引发此错误。
发现此,
- Error: Protocol error (Runtime.callFunctionOn): Execution context was destroyed.
- No page navigation lock ?
修复方法是(至少在我的情况下)等待 URL 更改,然后 page.evaluate
。
关于node.js - puppeteer 在上下文中评估结果被破坏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49806081/