title: getWindowHandle() Selenium Webdriver Javascript
body: <p>Made some changes based on help from engineering. Here is the final code I used for grabbing the new window handle:                                                                                                  </p>
  
  <pre><code>localdriver = @driver
  @driver.getAllWindowHandles()
  .then (handles) -&gt;
      localdriver.switchTo().window(handles[1])
  </code></pre>
  
  <p>I'm currently running an automation stack that uses Selenium Webdriver, Mocha, Chai, and Grunt. I'm creating scripts in Coffeescript, but an answer to my question in Javascript would be perfectly fine.</p>
  
  <p>What I'm trying to do:</p>
  
  <ul>
  <li>Click button on main browser window</li>
  <li>Switch driver to the second window that opens after button click</li>
  <li>Perform actions in the second window</li>
  <li>Close second window and return to the first.</li>
  </ul>
  
  <p>I've scoured the internet looking for an answer on how to do this. Just started learning all this stuff a few months ago, and I'm still stumbling through creating stuff. I'm seeing a lot of Java and C+ examples, but not much on the Javascript side. Can anyone provide an example of how to set up the code for the above scenario using Selenium Webdriver and Javascript?</p>