title: How to make Selenium WebDriver wait for page to load when new page is loaded via JS event
body: <p>I'm working on automating a site which has a number of links which load new pages via a JS event.  Basically, there are elements which are clickable, clicking on one causes some JavaScript to run and this leads to a form being submitted and routing to a new page.</p>
  
  <p>Now if these were just standard HTML links there would be no problem as Selenium is smart enough to tell that there's a new page coming and to wait to do things.  But as good as it is, Selenium can't tell that the clicks in this instance are leading to new pages to load so it doesn't wait and just keeps going.  As such it doesn't wait for the new page, tries to find elements which aren't there and my tests all fail.  Bummer.</p>
  
  <p>As a temporary solution I'm just pausing the program for three seconds like so:</p>
  
  <pre><code>oldPageDriver.clickThatButton();
  
  try {
    Thread.sleep(3000);
  } catch(InterruptedException ex) {
    Thread.currentThread().interrupt();
  }
  
  newPageDriver = new NewPageDriver(driver);
  newPageDriver.doStuffOnNewPage();
  </code></pre>
  
  <p>And this works, sort of.  I don't like it because it's "hacky," and just interrupting the program instead of doing something smarter.  Because the delay is hard coded at three seconds I get failures if the link is working but just slow.  I've considered something like an implicit wait but that accomplishes the same thing and I've not found a solid, workable answer in Java anywhere after a considerable amount of looking.</p>
  
  <p>So, can anyone suggest a way around this?  Specifically, how to make Selenium know that a new page is expected and to wait until it's available?</p>