title: How to switch to main window after closing a windows alert box in selenium using java
body: <p>I am using Selenium 3.7.1 and firefox latest version 67.0.2, I am actually trying to switch back to my home page after clicking a windows based popup(clicked Ok using AutoIT script). But after the popup closes I cannot switch back to the first window(i.. the home page). In all, there are two windows which are opened.</p>
  
  <p>I have using the code below to switch back to the home page without closing the parent window as I could not take the control back to the parent page. Recently, we had an upgrade in our project before that the code was working fine.</p>
  
  <pre><code>public void switchToNewWindow(int windowNumber) {
      Set&lt;String&gt; s = driver.getWindowHandles();
      Iterator&lt;String&gt; ite = s.iterator();
      int i = 1;
      while (ite.hasNext() &amp;&amp; i &lt; 10) {
          String popupHandle = ite.next().toString();
          driver.switchTo().window(popupHandle);
          System.out.println("Window title is : " + driver.getTitle());
          if (i == windowNumber)
              break;
          i++;
      }
  }
  </code></pre>
  
  <p>I am getting the following output: </p>
  
  <blockquote>
    <p>org.openqa.selenium.NoSuchWindowException: Browsing context has been discarded</p>
  </blockquote>