Q:

Selenium chromedriver losing window handles.  org.openqa.selenium. NoSuchWindowException: no such window error

I am using the following,
Chromedriver 2.41
selenium-server-standalone-3.10.0.jar
jdk 1.8.0_141
I use selenium to open multiple tabs in chrome, traverse each tab one by one and then execute some script.  However I often get errors while switching window handles.  Using System.out.println(driver.getWindowHandles()) just before switching windows, I have found that the number of window handles keeps on decreasing with time and it gives org.openqa.selenium. NoSuchWindowException: no such window.  
Another odd thing is, the code will run fine once.  Then it will give the error for subsequent tests, but if we leave it as is for ten mins later it will run fine again. 
The code implementation is as follows,
        ArrayList<String> tabs = new ArrayList<String>(driver.getWindowHandles());
        for(int j=0; j<10; j++){
            ((JavascriptExecutor) driver).executeScript("window.open('" + *siteURL* + "')");
            for(String temp : driver.getWindowHandles()){
                if(!tabs.contains(temp)){
                    driver.switchTo().window(temp);
                    tabs.add(temp);
                }
            }
        }

        tabs = new ArrayList<String>(driver.getWindowHandles());
        driver.switchTo().window(tabs.get(0));

        /*** Reload tabs to reacquire the pages- Selenium loses them if it is idle for long time
         * **/
            for (int j = 0; j < tabs.size(); j++) {
                driver.switchTo().window(tabs.get(j));
                driver.get(*siteURL*);

            }

        tabs = new ArrayList<String>(driver.getWindowHandles());
        for (int j = 0; j < tabs.size(); j++) {
          driver.switchTo().window(tabs.get(j));
         /*Perform step 1*/
        }

error:
org.openqa.selenium. NoSuchWindowException: no such window

(Session info: chrome=67.0.3396.87)
  (Driver info: chromedriver=2.41.578737 (49da6702b16031c40d63e5618de03a32ff6c197e),platform=Windows NT 6.1.7601 SP1 x86_64) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 0 milliseconds
Build info: version: '3.10.0', revision: '176b4a9', time: '2018-03-02T19:11:12.151Z'
System info: host: 'APACSGAWSBU0130', ip: '10.58.196.139', os.name: 'Windows 7', os.arch: 'x86', os.version: '6.1', java.version: '1.8.0_141'
Driver info: org.openqa.selenium.chrome. ChromeDriver
Capabilities {acceptInsecureCerts: false, acceptSslCerts: false, applicationCacheEnabled: false, browserConnectionEnabled: false, browserName: chrome, chrome: {chromedriverVersion: 2.41.578737 (49da6702b16031..., userDataDir: C:\Users\HY84957\Downloads\...}, cssSelectorsEnabled: true, databaseEnabled: false, goog:chromeOptions: {debuggerAddress: localhost:54006}, handlesAlerts: true, hasTouchScreen: false, javascriptEnabled: true, locationContextEnabled: true, mobileEmulationEnabled: false, nativeEvents: true, networkConnectionEnabled: false, pageLoadStrategy: normal, platform: XP, platformName: XP, rotatable: false, setWindowRect: true, takesHeapSnapshot: true, takesScreenshot: true, unexpectedAlertBehaviour: , unhandledPromptBehavior: , version: 67.0.3396.87, webStorageEnabled: true}
Session ID: 0f98cc640eea21f2c48120f3bb901846

A:

I have faced same issue.  It take some time to getWindowHandles(), with Thread.sleep(500); it's working fine. 
public void goto2() throws InterruptedException {
    ArrayList<String> winHandles = new ArrayList<String> (driver.getWindowHandles());
    Thread.sleep(500);
    driver.switchTo().window(winHandles.get(1));
}

