Skip to content

Crawlee—A web scraping and browser automation library for Node.js to build reliable crawlers. In JavaScript and TypeScript. Extract data for AI, LLMs, RAG, or GPTs. Download HTML, PDF, JPG, PNG, and other files from websites. Works with Puppeteer, Playwright, Cheerio, JSDOM, and raw HTTP. Both headful and headless mode. With proxy rotation.

fix: wait when `body` is not available in `infiniteScroll()` from Puppeteer utils (#1277)

Martin Adámek57987cde0429
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 53592dbde..32f08943f 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -3,6 +3,7 @@
- fix: ignore requests that are no longer in progress (#1258)
- fix: do not use `tryCancel()` from inside sync callback (#1265)
- fix: revert to puppeteer 10.x
+- fix: wait when `body` is not available in `infiniteScroll()` from Puppeteer utils
2.2.0 / 2021/12/17
====================
diff --git a/src/puppeteer_utils.js b/src/puppeteer_utils.js
index 10967cf0c..91f33706a 100644
--- a/src/puppeteer_utils.js
+++ b/src/puppeteer_utils.js
@@ -430,7 +430,19 @@ export const infiniteScroll = async (page, options = {}) => {
});
// Move mouse to the center of the page, so we can scroll up-down
- const body = await page.$('body');
+ let body = await page.$('body');
+ let retry = 0;
+
+ while (!body && retry < 10) {
+ await page.waitForTimeout(100);
+ body = await page.$('body');
+ retry++;
+ }
+
+ if (!body) {
+ return;
+ }
+
const boundingBox = await body.boundingBox();
await page.mouse.move(
boundingBox.x + boundingBox.width / 2, // x