sureshkumar k
sureshkumar k

Reputation: 3

unable to read child window element in selenium as parent window getting closed

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

Upvotes: 0

Views: 2069

Answers (2)

n__o
n__o

Reputation: 1729

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);

Upvotes: 1

srinivasulu reddy
srinivasulu reddy

Reputation: 16

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);
            }
        }

Upvotes: 0

Related Questions