-1

I have written the following code to open a google map window get it's url then close it and perform the actions on my previous window:

        try 
          {

           String winHandleBefore = driver.getWindowHandle();
            element("icon_google_map").click();
            for(String winHandle : driver.getWindowHandles())
             {
                driver.switchTo().window(winHandle);
             }
           Thread.sleep(9000);
            String currentURL = driver.getCurrentUrl();
            logMessage(currentURL);
            String expectedURL="xyz";
            Assert.assertEquals(currentURL, expectedURL);
            logMessage(currentURL);
            driver.close();
            driver.switchTo().window(winHandleBefore);
            return true;
    }
    catch(Exception e)
    {
        return false;
    }

The code above opens the map window but does not close it,instead it shows the following error when i perform the action on previous window.

Error: org.openqa.selenium.NoSuchWindowException: no such window: target window already closed

Rishu Saxena
  • 171
  • 1
  • 3
  • 19
  • possible duplicate of [selenium web driver - switch to parent window](http://stackoverflow.com/questions/11359118/selenium-web-driver-switch-to-parent-window) – Rishu Saxena Aug 18 '15 at 07:34

3 Answers3

0

you are closing the driver before switching so selenium can't find the window.

do switching first then close the driver.

driver.switchTo().window(winHandleBefore);

driver.close();

Santanu Sahoo
  • 1,137
  • 11
  • 29
0

Check the below line of code

for(String winHandle : driver.getWindowHandles())
             {
                driver.switchTo().window(winHandle);
             }

You are not switching to right window. You can do so by switching to second window like this.

driver.switchTo().window(getWindowHandles()[1]);
HemChe
  • 2,249
  • 1
  • 21
  • 33
0

Below code will switch the new window and will also the Google MAP window

for(String winHandle : driver.getWindowHandles())
             {
               driver.switchTo().window(getWindowHandles()[1]);
               driver.close();  // This will close the new open window
             }
Rupesh Shinde
  • 1,933
  • 12
  • 22