0

I am working with selenium automation. I have coded to login a page and it works fine.
The login passed and new child window was opened as a result and parent window was closed.

Due to that my web driver stops and result in exceptions.
Exception in thread main org.openqa.selenium.NoSuchWindowException:

No window found (WARNING: The server did not provide any stacktrace information)

Please help

Behzad
  • 3,502
  • 4
  • 36
  • 63

2 Answers2

1

Selenium keeps a list of windows (handles). The webdriver needs to point to the right handle. When a window is closed, its handle is deleted, and your driver will now point to something that doesn't exist anymore.

Likely, you have to explicitly switch window to point your driver to the right window handle. Maybe this could help:

  1. Switch between two browser windows using selenium webdriver
  2. Selenium python bindings: Moving between windows and frames

"new child window was opened as a result and parent window was closed"

The code below from (1) shows you how to retrieve the list of window handles, and retrieve the right handle based on its title.

private void handleMultipleWindows(String windowTitle) {
        Set<String> windows = driver.getWindowHandles();

        for (String window : windows) {
            driver.switchTo().window(window);
            if (driver.getTitle().contains(windowTitle)) {
                return;
            }
        }
    }

You can use that function (or something similar) to retrieve the new window. From the code you provided, this would give:

// Entering the credentials in the login window.
driver.findElement(By.id(txtUserId)).clear();
driver.findElement(By.id(txtUserId)).sendKeys(poovan);
driver.findElement(By.id(txtPassword)).clear();
driver.findElement(By.id(txtPassword)).sendKeys(welcome1);
driver.findElement(By.id(btnSubmit)).click();
// Here the login window gets closed, handler to that window disappears, and driver becomes stale.
// So we need update the driver to point to the new window
handleMultipleWindows("The title of my new window");
driver.findElement(By.name(bono)).sendKeys(080);
Community
  • 1
  • 1
n__o
  • 1,620
  • 15
  • 18
  • Thanks for the suggestion. i cannot able to switch the window. Exception in thread "main" org.openqa.selenium.NoSuchWindowException: Unable to find element on closed window (WARNING: The server did not provide any stacktrace information) Command duration or timeout: 10 milliseconds – sureshkumar k Aug 10 '15 at 11:32
  • Can you give us what code you used to try and switch windows. Updated the response. With a link to python doc (It's not java, but I find the explanations clearer that what's on SeleniumHQ's docs. – n__o Aug 10 '15 at 15:23
  • driver.findElement(By.id(txtUserId)).clear(); driver.findElement(By.id(txtUserId)).sendKeys(poovan); driver.findElement(By.id(txtPassword)).clear(); driver.findElement(By.id(txtPassword)).sendKeys(welcome1); driver.findElement(By.id(btnSubmit)).click(); driver.findElement(By.name(bono)).sendKeys(080); – sureshkumar k Aug 12 '15 at 08:49
  • when driver.findElement(By.id(btnSubmit)).click(); is clicked the parent form is geting closed. and new form is opened which i need to focus and driver.findElement(By.name(bono)).sendKeys(080); when i check the driver it shows NULL Thanks in Advance – sureshkumar k Aug 12 '15 at 08:51
  • There is no switch window in the code you provided. I updated my answer based on your code to illustrate the principle of *switching windows* in selenium. – n__o Aug 13 '15 at 16:30
  • when i click submit . my webdriver become null coz, the parent window is getting closed and if i try to switch window it shows No suchWindow Exception – sureshkumar k Aug 17 '15 at 12:20
  • I think you have all the elements/pointers to find the answer to your problem. You can try to print your driver before / after closing the window to make sure that it is indeed null. Otherwise you can try retrieving window handles before closing the window. In any case, I don't think there's anything more I can do to help at this point. Good luck! – n__o Aug 18 '15 at 04:23
  • I also have similar problem. In my case however, number of windowhandles is changing to zero – Sakshi Singla May 09 '19 at 07:01
0

Please use below code. It will work for sure.

String parentWindow = driver.getWindowHandle();
        Set<String> handles2 = driver.getWindowHandles();
        for (String windowHandle : handles2) {
            if (!windowHandle.equals(parentWindow)) {
                driver.switchTo().window(windowHandle);
            }
        }
prab2112
  • 1,024
  • 10
  • 36