'Unknown server side error while getting the child window title and to close the child window - Selenium, JAVA

I see that the Arraylist of driver.getWindowHandles() has 2 window ids [B52AFB77871844AA1266624ABA87D065, 4AE27B15D38DA8E709343F13B91D4F89] but below code to get child window title and to close child window is giving

An unknown server side error while processing the request.

driver.switchTo().window(child).getTitle();
driver.switchTo().window(child).close();

By commenting out these 2 lines and when I switch to parent window and continue the processing its working fine. and driver.quit() closes all browsers. But I want to get the title of the child window. Any inputs are really appreciated. Here is the complete code:

String parent = driver.getWindowHandle();
ArrayList<String> handles=new ArrayList<>(driver.getWindowHandles());

        for(int i=0;i<handles.size() ;i++)
        {
            String str=handles.get(i);
            if(str.equals(parent))
            {
               driver.switchTo().window(parent).getTitle()
            }
            else
            {
                driver.switchTo().window(str);
                driver.getTitle();
                driver.close();
                sleepNoError(4000);
                driver.switchTo().window(parent);        
            }


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source