title: Is using the method driver.close() required?
body: <p>Let's say </p>
  
  <ol>
  <li><p>I have a <code>window 1</code>. I have performed an event on the <code>window 1</code> that makes <code>window 2</code> to appear.</p></li>
  <li><p>Now I switched to the <code>window 2</code> and clicked a button on it which closes <code>window 2</code>.</p></li>
  </ol>
  
  <p>If I use <code>driver.close()</code> after I performed an event which caused the window to close, sometimes it throws <code>NoSuchWindowException</code>.</p>
  
  <p>If I don't use the <code>driver.close()</code> then sometimes  <code>driver.getWindowHandles().size()</code> returns <code>2</code> even when there is <em>only one window</em> and I have waited enough time to number of windows become <code>1</code>.</p>
  
  <p>I refresh <code>driver.getWindowHandles()</code> and check for the <code>driver.getWindowHandles().size()</code> to become <code>1</code> but it doesn't sometimes.</p>
  
  <p>My question is, do I need to use the method <code>driver.close()</code> after I clicked the button that caused the window to close? How to use the <code>driver.close()</code> correctly.</p>
  
  <p><strong>EDIT:</strong> Yes, it is a problem. If selenium doesn't realize <code>window2</code> has been closed, it keeps returning the handles to be <code>2</code>. Suppose that I closed <code>window2</code> and switched back to <code>window1</code> and performed an event which opens <code>window3</code>. Now I want to switch to <code>window3</code>. Here is the problem because Selenium still think <code>windows2</code> exists and now there are three windows according to the Selenium. </p>
  
  <pre><code>String window1Handle = driver.getWindowHandle();
  //Now I have oepend window3
  //According to the Selenium there are 3 windows
  // So driver.getWindowHandles().size() returns 3
   for (String window : driver.getWindowHandles() {
          if (!window.equals(window1Handle)) {
                    driver.switchTo().window();
  </code></pre>
  
  <p><em><strong>The above line may throw exception because driver is trying to switch to a window which has already been closed"</em></strong></p>