title: How can I close a specific window using Selenium WebDriver with Java?
body: <p>I use Selenium WebDriver. I open the first page then open the second page - perform some action and go back to first page. Before I want to close the second page I use the command <code>driver.close();</code>, but it closes the first page instead of the second. How can I make Selenium to close a specific window?</p>
  
  <p>Part of code</p>
  
  <pre><code>String HandleBefore = driver.getWindowHandle();
  
   driver.findElement(By.xpath("...")).click();
   for (String twohandle : driver.getWindowHandles()) {
          driver.switchTo().window(twohandle);
      }       
   driver.findElement(By.linkText("001")).click();
   driver.close();
  </code></pre>