title: Using Puppeteer, how can I open a page, get the data, then go back to the previous page to get the next page on the list?
body: <p><strong>SITUATION:</strong></p>
  
  <p>Here is what I want to do:</p>
  
  <p>1) I load page 0. Page 0 contains clickable links to different pages. I want to load the content of all those pages. So:</p>
  
  <p>2) Click on the first link. Load page 1. Get Data. Go back to the previous page (Page 0)</p>
  
  <p>3) Click on the second link which loads page 2. Etc.. ad infinitum until all links have been clicked.</p>
  
  <p>With my current code, page 0 loads, then the first link is clicked and loads page 1, then there is a crash with the following error: </p>
  
  <pre><code>(node:2629) UnhandledPromiseRejectionWarning: Error: Protocol error (Runtime.callFunctionOn): Execution context was destroyed.
  </code></pre>
  
  <hr>
  
  <p><strong>QUESTION:</strong></p>
  
  <p>What am I doing wrong and how can I make my script behave the way I intended ?</p>
  
  <hr>
  
  <p><strong>CODE:</strong></p>
  
  <pre><code>const puppeteer = require('puppeteer');
  const fs = require('fs');
  
  let getData = async () =&gt; {
      const browser = await puppeteer.launch({headless: false});
      const page = await browser.newPage();
  
      await page.goto('url', { waitUntil: 'networkidle2' });
      await page.setViewport({width: ..., height:...});
  
      const result = await page.evaluate(async () =&gt; {
          let data = []; 
          let elements = document.querySelector('.items').querySelectorAll('.item'); 
  
          for (const element of elements) {
  
              element.click();
              await new Promise((resolve) =&gt; setTimeout(resolve, 2000));
  
              // GETTING THE DATA THEN PUSHING IT INTO THE DATA ARRAY
  
              await page.goBack();
          }
  
          return data; // Return our data array
  
      });
  
      browser.close();
      return result; // Return the data
  };
  </code></pre>