title: Infinite loop with try catch puppeteer
body: <p>I have this code I made using Pupperteer to save URLs of Bing images, I have an Array of product names and I keep searching inside the Loop with the Try catch to get these URLs, but I have one problem, I wanted that when it didn't find the product (it didn't find a result) it would jump to the next item in the Array but it just doesn't do anything and Pupperteer closes the Browser entering the Finally of the Try catch, without pointing out any error. My logic seems right, can anyone help me with this question? I've tried everything</p>
  <p><strong>index.js</strong></p>
  <pre><code>const fs = require(&quot;fs&quot;);
  const puppeteer = require(&quot;puppeteer-core&quot;);
  
  (async () =&gt; {
    const browser = await puppeteer.launch({
      executablePath:
        &quot;C:\\Program Files (x86)\\Microsoft\\Edge\\Application\\msedge.exe&quot;,
      headless: true,
      timeout: 0,
    });
  
    const page = await browser.newPage();
    await page.setDefaultNavigationTimeout(0);
  
    let results = new Array();
  
    const arr = [
      {
        ID: 6011,
        Nome: &quot;BRA MASCARA BIGODE Q2321(KRIAT 13726)&quot;,
        Imagens: &quot;&quot;,
      },
      {
        ID: 6012,
        Nome: &quot;BRA MASCARA CAVEIRA ASSUSTADORA 1UN&quot;,
        Imagens: &quot;&quot;,
      },
      {
        ID: 6013,
        Nome: &quot;BRA MASCARA CAVEIRA PLASTICA  1UN&quot;,
        Imagens: &quot;&quot;,
      }
     ];
  
    await page.goto(`https://www.bing.com/search?q=Hello world`);
  
    await page.waitForSelector(&quot;#bnp_btn_accept&quot;, { visible: true });
  
    await page.evaluate(() =&gt; {
      const btn = document.getElementById(&quot;bnp_btn_accept&quot;);
      if (btn) {
        btn.click();
      }
    });
  
    let cont = 0;
  
    try {
      for (cont; cont &lt; arr.length; ) {
        let image;
  
        await page.goto(`https://www.bing.com/search?q=${arr[cont].Nome}`);
  
        await page.waitForTimeout(3000);
        await page.waitForSelector(&quot;#b-scopeListItem-images&quot;, { visible: true });
  
        await page.evaluate(() =&gt; {
          const imageBtn = document.getElementById(&quot;b-scopeListItem-images&quot;);
          imageBtn?.children[0]?.click();
        });
  
        await page.waitForTimeout(3000);
        await page.waitForSelector(&quot;.iusc&quot;, { visible: true });
  
        await page
          .evaluate(() =&gt; {
            const firstImage = JSON.parse(
              document.getElementsByClassName(&quot;iusc&quot;)[0].getAttribute(&quot;m&quot;)
            ).turl;
            const semResultado = document.getElementById(&quot;dg_nr&quot;);
            if (semResultado) {
              console.log(&quot;Não tem resultado&quot;);
            } else {
              return firstImage;
            }
          })
          .then(async (res) =&gt; {
            arr[cont].Imagens = await res;
  
            results.push(arr[cont]);
  
            var stream = fs.createWriteStream(&quot;results.json&quot;);
            stream.once(&quot;open&quot;, function (fd) {
              stream.write(JSON.stringify(results));
              stream.end();
            });
  
            console.log(&quot;Produto &quot; + cont + &quot; adicionado no Arquivo&quot;);
            cont++;
          })
          .catch((err) =&gt; {
            console.log(&quot;O Produto &quot; + cont + &quot; deu algum erro: &quot; + err);
            cont++;
          });
      }
    } catch (err) {
      cont++;
    } finally {
      var stream = fs.createWriteStream(&quot;results.json&quot;);
      stream.once(&quot;open&quot;, function (fd) {
        stream.write(JSON.stringify(results));
        stream.end();
      });
  
      await browser.close();
    }
  })();
  </code></pre>
  <p><strong>Fixed:</strong></p>
  <pre><code>const fs = require(&quot;fs&quot;);
  const puppeteer = require(&quot;puppeteer-core&quot;);
  
  (async () =&gt; {
    const browser = await puppeteer.launch({
      executablePath:
        &quot;C:\\Program Files (x86)\\Microsoft\\Edge\\Application\\msedge.exe&quot;,
      headless: true,
      timeout: 0,
    });
  
    const page = await browser.newPage();
    await page.setDefaultNavigationTimeout(0);
  
    let results = new Array();
  
    const arr = [
      {
        ID: 6011,
        Nome: &quot;BRA MASCARA BIGODE Q2321(KRIAT 13726)&quot;,
        Imagens: &quot;&quot;,
      },
      {
        ID: 6012,
        Nome: &quot;BRA MASCARA CAVEIRA ASSUSTADORA 1UN&quot;,
        Imagens: &quot;&quot;,
      },
      {
        ID: 6013,
        Nome: &quot;BRA MASCARA CAVEIRA PLASTICA  1UN&quot;,
        Imagens: &quot;&quot;,
      }
     ];
  
    await page.goto(`https://www.bing.com/search?q=Hello world`);
  
    await page.waitForSelector(&quot;#bnp_btn_accept&quot;, { visible: true });
  
    await page.evaluate(() =&gt; {
      const btn = document.getElementById(&quot;bnp_btn_accept&quot;);
      if (btn) {
        btn.click();
      }
    });
  
    let cont = 0;
  
    try {
      for (cont; cont &lt; arr.length; ) {
        let image;
  
        await page.goto(`https://www.bing.com/search?q=${arr[cont].Nome}`);
  
        await page.waitForTimeout(3000);
        await page.waitForSelector(&quot;#b-scopeListItem-images&quot;, { visible: true });
  
        await page.evaluate(() =&gt; {
          const imageBtn = document.getElementById(&quot;b-scopeListItem-images&quot;);
          imageBtn?.children[0]?.click();
        });
  
        await page.waitForTimeout(3000);
        await page.waitForSelector(&quot;.iusc&quot;, { visible: true }).catch(() =&gt; {
           cont++;
        });
  
        await page
          .evaluate(() =&gt; {
            const firstImage = JSON.parse(
              document.getElementsByClassName(&quot;iusc&quot;)[0].getAttribute(&quot;m&quot;)
            ).turl;
            const semResultado = document.getElementById(&quot;dg_nr&quot;);
            if (semResultado) {
              console.log(&quot;Não tem resultado&quot;);
            } else {
              return firstImage;
            }
          })
          .then(async (res) =&gt; {
            arr[cont].Imagens = await res;
  
            results.push(arr[cont]);
  
            var stream = fs.createWriteStream(&quot;results.json&quot;);
            stream.once(&quot;open&quot;, function (fd) {
              stream.write(JSON.stringify(results));
              stream.end();
            });
  
            console.log(&quot;Produto &quot; + cont + &quot; adicionado no Arquivo&quot;);
            cont++;
          })
          .catch((err) =&gt; {
            console.log(&quot;O Produto &quot; + cont + &quot; deu algum erro: &quot; + err);
            cont++;
          });
      }
    } finally {
      var stream = fs.createWriteStream(&quot;results.json&quot;);
      stream.once(&quot;open&quot;, function (fd) {
        stream.write(JSON.stringify(results));
        stream.end();
      });
  
      await browser.close();
    }
  })();
  </code></pre>