Puppeteer page.$$ yields no results

I’m encountering an issue using Puppeteer where the page.$$ method returns an empty array, despite the fact that the target website clearly has the desired elements when inspected with querySelectorAll in a browser console. I’ve attempted to use waitForSelector to ensure the elements load, but it only results in a timeout. Does anyone have insights into what might be causing this? Here is an alternative code snippet for clarity:

const fetchElements = async () => {
    try {
        const myBrowser = await puppeteer.launch({ args: ['--no-sandbox', '--disable-setuid-sandbox'] });
        const myPage = await myBrowser.newPage();
        await myPage.goto('https://example.com/sample');
        const foundElements = await myPage.$$('[data-target="item"]');
        console.log(foundElements);
        await myBrowser.close();
    } catch (err) {
        console.error('Encountered an error:', err);
    }
};

fetchElements();

I experienced similar issues where the Puppeteer page.$$ method returned empty arrays because the page was still loading dynamic content even after waitForSelector. In my case, I found that the JavaScript on the page was adding elements after an additional delay, so increasing the waiting time or switching to waiting for a network idle state was necessary. I was also surprised to learn that sometimes headless mode behaves differently than a full browser instance. Debugging with slowMo provided clearer insights into the timing issues.