title: Scroll down to bottom of infinite page with PhantomJS in Python
body: <p>I have succeeded in getting Python with Selenium and PhantomJS to reload a dynamically loading infinite scrolling page, like in the example below. But how could this be modified so that instead of setting a number of reloads manually, the program stopped when reaching rock bottom?</p>
  
  <pre><code>reloads = 100000 #set the number of times to reload
  pause = 0 #initial time interval between reloads
  driver = webdriver.PhantomJS()
  
  # Load Twitter page and click to view all results
  driver.get(url)
  driver.find_element_by_link_text("All").click()
  
  # Keep reloading and pausing to reach the bottom
  for _ in range(reloads):
      driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
      time.sleep(pause)
  
  text_file.write(driver.page_source.encode("utf-8"))
  text_file.close()
  </code></pre>