title: Playwright auto-scroll to bottom of infinite-scroll page
body: <p>I am trying to automate the scraping of a site with &quot;infinite scroll&quot; with Python and Playwright.</p>
  <p>The issue is that Playwright doesn't include, as of yet, a scroll functionnality let alone an infinite auto-scroll functionnality.</p>
  <p>From what I found on the net and my personnal testing, I can automate an infinite or finite scroll using the <code>page.evaluate()</code> function and some Javascript code.</p>
  <p>For example, this works:</p>
  <pre><code>for i in range(20):
      page.evaluate('var div = document.getElementsByClassName(&quot;comment-container&quot;)[0];div.scrollTop = div.scrollHeight')
      page.wait_for_timeout(500)
  </code></pre>
  <p>The problem with this approach is that it will either work by specifying a number of scrolls or by telling it to keep going forever with a <code>while True</code> loop.</p>
  <p>I need to find a way to tell it to keep scrolling until the final content loads.</p>
  <p>This is the Javascript that I am currently trying in <code>page.evaluate()</code>:</p>
  <pre><code>var intervalID = setInterval(function() {
      var scrollingElement = (document.scrollingElement || document.body);
      scrollingElement.scrollTop = scrollingElement.scrollHeight;
      console.log('fail')
  }, 1000);
  var anotherID = setInterval(function() {
      if ((window.innerHeight + window.scrollY) &gt;= document.body.offsetHeight) {
          clearInterval(intervalID);
      }}, 1000)
  </code></pre>
  <p>This does not work either in my firefox browser or in the Playwright firefox browser. It returns immediately and doesn't execute the code in intervals.</p>
  <p>I would be grateful if someone could tell me how I can, using Playwright, create an auto-scroll function that will detect and stop when it reaches the bottom of a dynamically loading webpage.</p>