title: Selenium Javascript Wait
body: <p>I'm trying to use selenium-webdriver in Node to crawl Google finance pages. The driver.wait function does not appear to work as expected. I have set my mocha timeout to be 10 seconds and the driver.wait timeout be 9 seconds. The test passes about half of the time, but when it fails, it doesn't take anywhere near 9 seconds to fail - it actually fails in about 1 second and then takes another 8 before closing the test. I'm obviously missing something, but I've included the commented-out iterations of different things I've tried in order to make this work (including setTimeout). If anyone can help me see the error in my thinking, I would be much obliged. Here's the code:</p>
  
  <pre><code>(function () {
      var assert = require("chai").assert;
      var webdriver = require("selenium-webdriver");
      var urlGoogleFinanceRoot = "https://www.google.com/finance";
  
  describe("Selenium", function () {
      it("should fetch a couple of pages and keep all of the content", function (done) {
          var driver = new webdriver.Builder().withCapabilities(webdriver.Capabilities.chrome()).build();
          webdriver.promise.controlFlow().on("uncaughtException", function (e) {
              console.error("Error1: " + e);
          });
          // driver.get(urlGoogleFinanceRoot + "?q=BAC").then(function () {
          //  return setTimeout(function () {
          //      return driver.findElement(webdriver.By.xpath("//table[@class='snap-data']")).isDisplayed();
          //  }, 9000).then(function (isDisplayed) {
          //      assert.isTrue(isDisplayed);
          //      driver.quit();
          //      done();
          //  });
          // });
  
          // driver.wait(function () {
          //  return driver.get(urlGoogleFinanceRoot + "?q=BAC").then(function () {
          //      return driver.wait(function () {
          //          return driver.findElement(webdriver.By.xpath("//table[@class='snap-data']")).isDisplayed();
          //      }, 9000);
          //  });
          // }, 9000).then(function (isDisplayed) {
          //  assert.isTrue(isDisplayed);
          //  driver.quit();
          //  done();
          // });
          // driver.wait(function(){
          //  return driver.get(urlGoogleFinanceRoot + "?q=BAC").then(function(){
          //      return driver.findElement(webdriver.By.xpath("//table[@class='snap-data']")).isDisplayed();
          //  });
          // },5000).then(function(isDisplayed){
          //  assert.isTrue(isDisplayed);
          //  driver.quit();
          //  done();
          // });
          driver.get(urlGoogleFinanceRoot + "?q=BAC").then(function () {
              driver.wait(function () {
                  return driver.findElement(webdriver.By.xpath("//table[@class='snap-data']")).isDisplayed();
              }, 9000).then(function (isReady) {
                  assert.isTrue(isReady);
                  driver.quit();
                  done();
              });
              });
          });
      });
  })();
  </code></pre>
  
  <p>and here's the output:</p>
  
  <pre><code>  Selenium
  Error1: NoSuchElementError: no such element
    (Session info: chrome=44.0.2403.107)
    (Driver info: chromedriver=2.16.333243      (0bfa1d3575fc1044244f21ddb82bf870944ef961),platform=Linux 3.16.0-4-amd64  x86_64)
      1) should fetch a couple of pages and keep all of the content
  
  
    0 passing (10s)
    1 failing
  
    1) Selenium should fetch a couple of pages and keep all of the content:
       Error: timeout of 10000ms exceeded. Ensure the done() callback is being called in this test.
  </code></pre>