title: Selenium check if a window is currently open
body: <p>So I have a selenium webdriver logging me into a website and from there I click a button which opens a second window. I use this code below to switch to the new window</p>
  
  <pre><code> String winParent = driver.getWindowHandle();
  
   for (String winHandle : driver.getWindowHandles()) {
          driver.switchTo().window(winHandle);
   }
  </code></pre>
  
  <p>Now on that second window I run some automation . Once complete I would press the save button and this would close the current window. If the window doesn't close it means I have some errors on the page and I need to loop back and fix it.</p>
  
  <pre><code> driver.findElement(By.id("btnSave")).click();
  
    if (isAlive(driver) == false) {
        //break and exit
        System.out.println("cic" + name);
        finalString = finalString + "cic: " + name;
        break;
  
     }
  
  
    public Boolean isAlive(WebDriver driver) {
      try {
          driver.getCurrentUrl();//or driver.getTitle();
          return true;
      } catch (Exception ex) {
          return false;
      }
  }
  </code></pre>
  
  <p>The The program works as expected when it catches the errors and the window doesn't close. But as soon as everything is clear and the window closes it enters the if statement above and displays this error.</p>
  
  <p>Unable to receive message from renderer.</p>
  
  <p>I believe that I'm not checking if the window has been closed correctly.</p>
  
  <p>edit: after some debugging it seems like once the window closes the program can't really tell what to do next. <a href="https://i.imgur.com/l8nsPPr.png" rel="nofollow noreferrer">http://i.imgur.com/l8nsPPr.png</a></p>