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.
TypeScript apifyautomationcrawlercrawlingheadlessheadless-chromejavascriptnodejsnpmplaywrightpuppeteerscraperscrapingtypescriptweb-crawlerweb-crawlingweb-scraping
Feature/update infinite scroll (#705)
Lukáš Křivka5c009cf4aeda
diff --git a/src/puppeteer_utils.js b/src/puppeteer_utils.jsindex 23f94fb53..a37a2cbdf 100644--- a/src/puppeteer_utils.js+++ b/src/puppeteer_utils.js@@ -449,16 +449,22 @@ export const gotoExtended = async (page, request, gotoOptions = {}) => {* How many seconds to scroll for. If 0, will scroll until bottom of page.* @param {number} [options.waitForSecs=4]* How many seconds to wait for no new content to load before exit.+ * @param {Boolean} [options.scrollDownAndUp=false]+ * If true, it will scroll up a bit after each scroll down. This is required on some websites for the scroll to work.+ * @param {String} [options.buttonSelector]+ * Optionally checks and clicks a button if it appears while scrolling. This is required on some websites for the scroll to work.* @returns {Promise<void>}* @memberOf puppeteer* @name infiniteScroll*/export const infiniteScroll = async (page, options = {}) => {- const { timeoutSecs = 0, waitForSecs = 4 } = options;+ const { timeoutSecs = 0, waitForSecs = 4, scrollDownAndUp = false, buttonSelector } = options;checkParamOrThrow(page, 'page', 'Object');checkParamOrThrow(timeoutSecs, 'timeoutSecs', 'Number');checkParamOrThrow(waitForSecs, 'waitForSecs', 'Number');+ checkParamOrThrow(scrollDownAndUp, 'scrollDownAndUp', 'Boolean');+ checkParamOrThrow(buttonSelector, 'buttonSelector', 'Maybe String');let finished;const startTime = Date.now();@@ -504,8 +510,25 @@ export const infiniteScroll = async (page, options = {}) => {}, SCROLL_HEIGHT_IF_ZERO);};+ const maybeClickButton = async () => {+ const button = await page.$(buttonSelector);+ // Box model returns null if the button is not visible+ if (button && await button.boxModel()) {+ await button.click({ delay: 10 });+ }+ };+while (!finished) {await doScroll();+ await page.waitFor(50);+ if (scrollDownAndUp) {+ await page.evaluate(() => {+ window.scrollBy(0, -1000);+ });+ }+ if (buttonSelector) {+ await maybeClickButton();+ }}};