title: Puppeteer: multiple screenshots in one browser instance
body: <p>so I want to screenshot a specific <code>class</code> multiple times, but all the time it would say <code>Session Closed</code> or <code>Terminated</code>, therefore I worked around implementing multiple screenshots opening multiple instances.</p>
  
  <p>Could someone at least guide how to multiple instances on the same browser instance? </p>
  
  <p><code>my code</code>:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>const puppeteer = require("puppeteer");
  
  const SELECTOR = ".octicon";
  
  (async () =&gt; {
    let screenshotNumber = 0;
    async function screenshots() {
      const browser = await puppeteer.launch({
        headless: true
      });
  
      try {
        let page = await browser.newPage();
        page.setViewport({ width: 1000, height: 600, deviceScaleFactor: 2 });
  
        await page.goto("https://github.com/");
        await page.waitForNavigation({ waitUntil: "networkidle" });
  
        const rect = await page.evaluate(selector =&gt; {
          const element = document.querySelector(selector);
          const { x, y, width, height } = element.getBoundingClientRect();
          return { left: x, top: y, width, height, id: element.id };
        }, SELECTOR);
  
        await page.screenshot({
          path: `octicon-${screenshotNumber}.jpg`,
          clip: {
            x: rect.left,
            y: rect.top,
            width: rect.width,
            height: rect.height
          }
        });
  
        browser.close();
        screenshotNumber++;
      } catch (e) {
        console.log(e);
        browser.close();
      }
    }
  
    async function run() {
      await screenshots();
      setTimeout(run, 200);
    }
  
    run();
  })();</code></pre>
  </div>
  </div>
  </p>