title: how to close a pop up window on a web page using webdriver
body: <p>&lt;Insert question here.&gt;</p>
  
  <pre><code>public class Day4
  {
      @Test
      public void main() throws InterruptedException
      {
          WebDriver Driver = new FirefoxDriver();
          Driver.get("http://live.guru99.com");
          Driver.manage().window().maximize();
          Thread.sleep(6000);
          WebElement element = Driver.findElement(By.xpath("//a[text()='Mobile']"));
          element.click();
          element = Driver.findElement(By.xpath("//a[text()='IPhone']/../../div[@class='actions']//a[text()='Add to Compare']"));
          Thread.sleep(2000);
          element.click();
          element = Driver.findElement(By.xpath("//a[text()='Samsung Galaxy']/../../div[@class='actions']//a[text()='Add to Compare']"));
          Thread.sleep(2000);
          element.click();
          element = Driver.findElement(By.xpath("//button[@title='Compare']"));
          element.click();
          element = Driver.findElement(By.xpath("//button[@title='Close Window']"));
          Thread.sleep(6000);
          element.click();
          Set&lt;String&gt; windowId = Driver.getWindowHandles(); // get window id of current window
          Iterator&lt;String&gt; itererator = windowId.iterator();
  
          String mainWinID = itererator.next();
          String newAdwinID = itererator.next();
  
          Driver.switchTo().window(newAdwinID);
          System.out.println(Driver.getTitle());
          Thread.sleep(3000);
          Driver.close();
  
          Driver.switchTo().window(mainWinID);
          System.out.println(Driver.getTitle());
          Thread.sleep(2000);
  
          Driver.close();
      }
  }
  </code></pre>