In Puppeteer, how do I loop through elements, fetch data, and return to the original page?

Using Puppeteer, I collect elements with a given class, click each to retrieve details from a new page, then navigate back.

await page.goto('https://example.com/start', { waitUntil: 'networkidle0', timeout: 120000 });
const items = await page.$$('.item-wrapper');
for (const item of items) {
  const desc = await item.$eval('.description', el => el.textContent);
  await item.click();
  await page.waitForSelector('.result-info');
  const result = await page.$eval('.result-info', el => el.textContent);
  console.log({ desc, result });
  await page.goBack();
}

hey, ive found that saving detail urls first, then using page.goto() instead of goBack() works better. somtimes it avoids stale element errors and keeps the flow more stable in puppeteer.

Based on my experience, I encountered several timing issues using goBack() in Puppeteer. I eventually decided to re-fetch elements after returning to the original page, which reduces the chance of selecting stale elements. For instance, I insert an explicit wait before fetching the list again. I also found that navigating with page.goto() combined with dynamic URL retrieval renders the process more robust. This approach may take a bit more initial work to set up, but it resolves many unexpected behaviors that arise from navigating back and forth in a dynamic DOM.