title: How do I catch Selenium Errors using WebDriverJS
body: <p>So I am using the JavaScript implementation of Selenium, WebDriverJS. As with any web browser automation the biggest hurdle is getting the code slow down long enough for page elements to load. My solution is this:</p>
  
  <p>for each element I want to interact with I have a code block like this</p>
  
  <pre><code>xpath = "//div[@id='gs_lc0']/input[@id='gbqfq']"
  driver.wait(function(){
      return waitForElement(xPath,driver);
  });
  try{
      element = driver.findElement(webdriver.By.xpath(xPath));
  }catch(e){
      console.log("Wait Function False Positive")
  }
  element.sendKeys("Let Me Google That For You\n";
  </code></pre>
  
  <p>with this as the function repeated in the wait function</p>
  
  <pre><code>var waitForElement = function(path, driver){
      console.log("try: " + path)
      try{
          driver.findElement(webdriver.By.xpath(path));
      }catch (e){
          console.log("FAILURE")
          return false;
      }
      console.log("SUCCESS")
      return true;
  }
  </code></pre>
  
  <p>now this code will work sometimes but othertimes it won't. I suppect the wait function isn't working at all and I'm just getting lucky webpage load times. So to test this theory I added the try function to the code block I cant even get the "NoSuchElementError" to be caught. So the brunt of my question is if there is some other way to form the tryCatch function so these errors will be caught.</p>
  
  <p>Also here is what the head of my code looks like if want a full reproduction</p>
  
  <pre><code>var webdriver = require('selenium-webdriver'), element
  
  var driver = new webdriver.Builder().
      withCapabilities(webdriver.Capabilities.chrome()).
      build();
  
  driver.get('google.com');
  </code></pre>