title: Scrolling multiple pages with NodeJS Puppeteer but only 1 page got scrolled and the others don't
body: <p>This code here successfully scrolls through the 1 page then terminate, and only briefly touches others then promptly ignore them. I think it might have something to do with <code>page</code> variable and somehow it got mixed up during asynchronous execution.</p>
  <pre><code>const puppeteer = require('puppeteer')
  
  const scrollElement = 'document.getElementsByClassName(&quot;item-view&quot;)[0]'
  const urls = [&quot;https://batdongsan.com.vn/ban-do-nha-dat-ban#ptype=38&amp;cat=&amp;city=&amp;district=&amp;area=&amp;price=&amp;ward=&amp;street=&amp;room=&amp;direction=&amp;project=&amp;points=10.77819980508061:106.71007824963644,10.783511697967592:106.7051000697048,10.78570388039108:106.70115185803488,10.779042968942315:106.68965054577902,10.770864179835455:106.68192578381613,10.762769489461716:106.67849255627706,10.755686457050816:106.68235493725851,10.752819468066056:106.68595982617452,10.753662702948837:106.69231129712179,10.761504674449494:106.70063687390402,10.769683717701703:106.70904828137472,10.775417347579069:106.71230984753683&amp;zoom=14&amp;center=10.764076459398622:106.70570231730571&amp;page=1&amp;product=&amp;searchtype=1&quot;,&quot;https://batdongsan.com.vn/ban-do-nha-dat-ban#ptype=38&amp;cat=&amp;city=&amp;district=&amp;area=&amp;price=&amp;ward=&amp;street=&amp;room=&amp;direction=&amp;project=&amp;points=21.036610658726687:105.85618505276071,21.03933437995124:105.85223684109079,21.038373072264537:105.84657201565133,21.0334863289415:105.84245214260446,21.029160225699314:105.84090719021188,21.026195971345416:105.8433962801777,21.0240328296118:105.84983358181344,21.02411294653207:105.8547259310566,21.026676665245493:105.85721502102243,21.03124317997471:105.85807332790719,21.034688001997072:105.856957528957&amp;zoom=14&amp;center=21.027144004643514:105.86811695124052&amp;page=1&amp;product=&amp;searchtype=1&quot;];
  let alias = {};
  alias[urls[0]] = &quot;URL1&quot;;
  alias[urls[1]] = &quot;URL2&quot;;
  
  (async () =&gt; {
    const browser = await puppeteer.launch({headless:false, slowMo:0})
    await Promise.all(urls.map(async (url)=&gt;{
      const page = await browser.newPage()
      await page.goto(url)
      await page.waitForSelector(&quot;.detail-title&quot;)
      let previousHeight
      while (true) {
        try {
          previousHeight = await page.evaluate(`${scrollElement}.scrollHeight`);
          console.log(`Scroll ${alias[url]} to ${previousHeight}`);
          await page.evaluate(`${scrollElement}.scrollTo(0, ${scrollElement}.scrollHeight)`);
          await page.waitForFunction(`${scrollElement}.scrollHeight &gt; ${previousHeight}`, {timeout:1000})
        } catch (e) {
          if (e instanceof puppeteer.errors.TimeoutError) {
            console.log(&quot;Finished scrolling&quot;)
            break
          } else {
            throw(e)
          }
        }
      }
      parseList = await page.$$(&quot;li.detail-item&quot;)
      console.log(parseList.length);
    }))
    await browser.close()
  })();
  </code></pre>
  <p>Console log on my machine</p>
  <pre><code>Scroll URL2 to 588
  Scroll URL2 to 1176
  Scroll URL2 to 1782
  Scroll URL2 to 2320
  Scroll URL2 to 2890
  Scroll URL2 to 3452
  Scroll URL2 to 4040
  Scroll URL1 to 624 // scroll once then never again
  Scroll URL2 to 4628
  Scroll URL2 to 5162
  Scroll URL2 to 5722
  Scroll URL2 to 6262
  Scroll URL2 to 6848
  Scroll URL2 to 7420
  Scroll URL2 to 8008
  Scroll URL2 to 8614
  Scroll URL2 to 9202
  Scroll URL2 to 9762
  Scroll URL2 to 10368
  Scroll URL2 to 10974
  Scroll URL2 to 11544
  Scroll URL2 to 12122
  Scroll URL2 to 12694
  Scroll URL2 to 13264
  Scroll URL2 to 13836
  Scroll URL2 to 14390
  Scroll URL2 to 14996
  Scroll URL2 to 15602
  Scroll URL2 to 16198
  Scroll URL2 to 16786
  Scroll URL2 to 17356
  Scroll URL2 to 17908
  Scroll URL2 to 18486
  Scroll URL2 to 19074
  Scroll URL2 to 19680
  Scroll URL2 to 20200
  Scroll URL2 to 20770
  Scroll URL2 to 21330
  Scroll URL2 to 21918
  Scroll URL2 to 22524
  Scroll URL2 to 23112
  Scroll URL2 to 23684
  Scroll URL2 to 24238
  Scroll URL2 to 24726
  Scroll URL2 to 25296
  Scroll URL2 to 25920
  Scroll URL2 to 26508
  Scroll URL2 to 27060
  Scroll URL2 to 27620
  Scroll URL2 to 28180
  Scroll URL2 to 28758
  Scroll URL2 to 29312
  Scroll URL2 to 29866
  Scroll URL2 to 30472
  Scroll URL2 to 31008
  Scroll URL2 to 31578
  Scroll URL2 to 32120
  Scroll URL2 to 32690
  Scroll URL2 to 33258
  Scroll URL2 to 33818
  Scroll URL2 to 34424
  Scroll URL2 to 35012
  Scroll URL2 to 35564
  Scroll URL2 to 36152
  Finished scrolling
  4 // there should be more than 500 items here
  Scroll URL2 to 36740
  Scroll URL2 to 37318
  Scroll URL2 to 37878
  Scroll URL2 to 38456
  Scroll URL2 to 39044
  Scroll URL2 to 39598
  Scroll URL2 to 40102
  Scroll URL2 to 40606
  Scroll URL2 to 41176
  Scroll URL2 to 41764
  Scroll URL2 to 42336
  Scroll URL2 to 42890
  Scroll URL2 to 43460
  Scroll URL2 to 44004
  Scroll URL2 to 44592
  Scroll URL2 to 45134
  Scroll URL2 to 45676
  Scroll URL2 to 46212
  Scroll URL2 to 46782
  Scroll URL2 to 47360
  Scroll URL2 to 47912
  Scroll URL2 to 48466
  Scroll URL2 to 49054
  Scroll URL2 to 49624
  Scroll URL2 to 50194
  Scroll URL2 to 50772
  Scroll URL2 to 51360
  Scroll URL2 to 51902
  Scroll URL2 to 52396
  Scroll URL2 to 52932
  Scroll URL2 to 53458
  Scroll URL2 to 53994
  Scroll URL2 to 54504
  Scroll URL2 to 55092
  Scroll URL2 to 55662
  Scroll URL2 to 56232
  Scroll URL2 to 56802
  Scroll URL2 to 57390
  Scroll URL2 to 57960
  Scroll URL2 to 58530
  Scroll URL2 to 59082
  Scroll URL2 to 59634
  Scroll URL2 to 60194
  Scroll URL2 to 60800
  Scroll URL2 to 61354
  Scroll URL2 to 61908
  Scroll URL2 to 62462
  Scroll URL2 to 62996
  Scroll URL2 to 63584
  Scroll URL2 to 64122
  Scroll URL2 to 64642
  Scroll URL2 to 65230
  Scroll URL2 to 65818
  Scroll URL2 to 66350
  Scroll URL2 to 66920
  Scroll URL2 to 67472
  Scroll URL2 to 68050
  Scroll URL2 to 68604
  Scroll URL2 to 68748
  Finished scrolling
  485
  </code></pre>