6

In the attached screenshot,i want to click New Browser Window. I want to close the browser window and click on New Message window. I closed the browser window. But i am getting the exception

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

Screenshot

Below are the details Screenshot

Below is the code

@Test
public void testing()
{
    driver.manage().window().maximize();
    driver.get("http://www.seleniumframework.com/Practiceform/");
    driver.findElement(By.id("button1")).click();
    Set<String> handle=driver.getWindowHandles();
    for(String handles:handle){
       try{
          String text=driver.switchTo().window(handles).getPageSource();
          if(text.contains("Agile Testing and ATDD Automation")){
              System.out.println("Text found");
              driver.close();
              break;
          }
       }catch(Exception e){}
    }
    driver.switchTo().defaultContent();
    driver.findElement(By.xpath("//button[contains(text(),'New Message Window')]")).click();
    driver.quit();
Mzf
  • 5,210
  • 2
  • 24
  • 37
Manish B
  • 389
  • 1
  • 4
  • 19
  • Possible duplicate of [How to switch to the new browser window, which opens after click on the button](http://stackoverflow.com/questions/9588827/how-to-switch-to-the-new-browser-window-which-opens-after-click-on-the-button) – Saurabh Gaur Sep 16 '16 at 13:20

2 Answers2

10

I guess you try to back to original window using driver.switchTo().defaultContent(); ? This is not proper way.

You should:

  1. store the original window at the beginning of test:

String winHandleBefore = driver.getWindowHandle();

  1. click button, switch windows, do whatever

  2. To back to original window use:

driver.switchTo().window(winHandleBefore);

Answer based on:https://stackoverflow.com/a/9597714/4855333

Community
  • 1
  • 1
kotoj
  • 769
  • 1
  • 6
  • 25
  • 1
    Python syntax: https://stackoverflow.com/questions/10629815/how-to-switch-to-new-window-in-selenium-for-python – jsf80238 Jul 26 '20 at 15:08
1

In your code, Set<String> handle=driver.getWindowHandles(); will contains list of handles from the browser. this will also contains the window handle of current tab in its first position.

so when you loop gets executed for the first time,the focus will be on the current window only.so as your code executes it closes the current window.

You need to get the current window handle first and before switching to any window just check whether the window that you need to switch is not current window handle.

Look at below example.

String mainwindow = driver.getWindowHandle();
Set<String> handle=driver.getWindowHandles();
for(String handles:handle)
{
   if(!mainwindow.equals(handles)){
      try
    {
         String text=driver.switchTo().window(handles).getPageSource();
         if(text.contains("Agile Testing and ATDD Automation"))
            {
               System.out.println("Text found");
               driver.close();
               break;
             }
   }catch(Exception e)
      {

      }
   }
}
Sudharsan Selvaraj
  • 4,792
  • 3
  • 14
  • 22