I have a written a Geb script for automation and I need to handle the Popup. but when switching the child window to parent window am getting a No Such Window Exception

Below is my code:

def AcceptSecurity(){
    log.info("Click Next")
        String parentWindowHandler = browser.getAvailableWindows() // Store your parent window
            String subWindowHandler = null

        Set<String> handles = browser.getAvailableWindows() // get all window handles
        Iterator<String> iterator = handles.iterator()
        while (iterator.hasNext()){
            subWindowHandler = iterator.next()
        }
            browser.switchToWindow(subWindowHandler) // switch to popup window
                SecurityNext.click()
                log.info("Get the Parent Window")
                browser.switchToWindow(parentWindowHandler)  // switch back to parent window

        }

If you look at the source code getAvaivableWidnwos is basically an alias to getWindowHandles, which returns a Set, not a String so I am not sure if you can use it as you do. So either get the first element of the set or use plain old selenium API. So instead of

String parentWindowHandler = browser.getAvailableWindows();

try

String parentWindowHandler = browser.getWindowHandle();



Next thing I would like to address are the { brackets }. Are you sure by the bracket:

        subWindowHandler = iterator.next()
    }

This means you would iterate through all the windows and at the end of the iteration, you would use the last one to switch to, click, and switch back to main. So presumably you wanted something like:

def AcceptSecurity(){
    log.info("Click Next")
    String parentWindowHandler = browser.getWindowHandle() // Store your parent window
    String subWindowHandler = null

    Set<String> handles = browser.getAvailableWindows() // get all window handles
    Iterator<String> iterator = handles.iterator()
    while (iterator.hasNext()){
        subWindowHandler = iterator.next()

        browser.switchToWindow(subWindowHandler) // switch to popup window
        SecurityNext.click()
        log.info("Get the Parent Window")
        browser.switchToWindow(parentWindowHandler)  // switch back to parent window

    }
}

Basically, I just cleaned code formatting. Trust in it, it is a powerful thing.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.