Automating Infinite Scroll with Puppeteer

I need to scroll a dynamic container (#left_container_scroll) in Puppeteer. My attempt throws a ReferenceError for an undefined variable. Try this:

const targetId = '#left_container_scroll';
let containerEl = await page.evaluate(selector => document.querySelector(selector), targetId);
const oldScrollHeight = await page.evaluate(el => el.scrollHeight, containerEl);
await page.evaluate(el => el.scrollTo(0, el.scrollHeight), containerEl);
await page.waitForFunction((prevHeight, selector) => document.querySelector(selector).scrollHeight > prevHeight, {}, oldScrollHeight, targetId);

I once faced a similar problem when automating a dynamic scroll with Puppeteer. I found that the key issue was context management: sometimes variables that exist in the Node context do not pass correctly to the browser context inside page.evaluate. I solved it by re-evaluating the element on each call, ensuring that I always got the correct reference. Also, verifying that the selector was correctly referenced within the page proved crucial. In my case, restructuring the scroll logic in a loop where the element was repeatedly fetched helped overcome the limitation.