title: Scrolling a page with AJAX content loading Selenium Webdriver
body: <p>I'm using Selenium Webdriver to get the content of a webpage. The page loads more content as the user scrolls down using AJAX. I am able to scroll down using javascript, but don't know when to stop scrolling. How do I know when I can't scroll down anymore? I can't use document.clientheight or any of the height properties, because they deal with scrolling when the window is too small but the elements are all attached to the DOM and their height is therefore known. Here, the elements are being dynamically attached to the DOM, so the height is not known in advance.</p>
  
  <p>How do I know when I've reached the bottom? Is there a "scrollbar is not scrollable" condition available somewhere? Or will that also be triggered if more content is loading and the scrollbar is not yet available to scroll, but soon will be?</p>
  
  <p>Thanks,
  bsg</p>
  
  <h2>Update</h2>
  
  <p>The answer that worked for me was actually given to another question of mine (this one was a more precise subset of that one). Namely, use jquery to test the following equivalence: $(document).height() == ($(window).height() + $(window).scrollTop();. If it evaluates to true, you've reached the bottom of the page. (For an explanation of why this works, see <a href="https://stackoverflow.com/questions/15690541/meaning-of-window-scrolltop-document-height-window-height">Meaning of $(window).scrollTop() == $(document).height() - $(window).height()</a>) I'm not sure if this would work without jquery, so if the page you're testing doesn't have jquery, you probably should use user1177636's answer below.</p>
  
  <p>The exact code I used was:</p>
  
  <pre><code>JavascriptExecutor js = (JavascriptExecutor)driver;
  do{
  
    //scroll down and do whatever processing you need
  
    reachedbottom = Boolean.parseBoolean(js.executeScript("return $(document).height() == ($(window).height() + $(window).scrollTop());").toString());
  
    }while(!reachedbottom);
  </code></pre>
  
  <p>Hope this helps someone.</p>