[英]Document and Window are not Defined Puppeteer Electron
我遇到了一個問題,我在 electron 應用程序的后端使用 puppeteer 運行自動 chrome session,但我不斷收到錯誤文檔未定義。 這是代碼:
const isDone = await this.page.evaluate(async () => await this.autoScroll);
...
async autoScroll() {
return new Promise(resolve => {
const timer = setInterval(() => {
document.scrollingElement.scrollBy(0, 100);
if (document.scrollingElement.scrollTop + window.innerHeight >= document.scrollingElement.scrollHeight) {
clearInterval(timer);
resolve(true);
}
}, 230);
});
}
如果我在不同的文件夾中單獨運行代碼,則代碼可以完美運行,但是當我在應用程序中運行它時,它似乎會出錯。
歡迎任何幫助。
謝謝。
我正在用 babel 轉換我的代碼。 Puppeteer page.evaluate 調用 autoScroll.toString() 與 babel 一起使用時會導致字符串不完整。 相反,它適用於:
const isDone = await this.page.evaluate(`(async() => {
return new Promise(resolve => {
const timer = setInterval(() => {
document.scrollingElement.scrollBy(0, 100);
if (document.scrollingElement.scrollTop + window.innerHeight >= document.scrollingElement.scrollHeight) {
clearInterval(timer);
resolve(true);
}
}, 230);
});
})()`);
只需將它放在一個字符串中,這樣 toString 就沒有任何問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.