5

I have a test case with following scenario:

1) Navigate to website.
2) Enter Login Credential.
3) Click on Login.

After login in my application, login window is closed by system and new child window is the homepage. I am unable to interact with the elements of the new child window.

public class testRegister {
    public static void main(String[] args) throws InterruptedException {

        int i = 0;
        ArrayList<String> ar = new ArrayList<String>();

        System.setProperty("webdriver.ie.driver","E:/Jigar Old Back up/Selenium/IEDriverServer.exe");
        System.setProperty("webdriver.ie.driver.loglevel", "TRACE");
        System.setProperty("webdriver.ie.driver.logfile", "E:/selenium.log");
        DesiredCapabilities ieCapabilities = DesiredCapabilities.internetExplorer();
        ieCapabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS,true);


        WebDriver driver = new InternetExplorerDriver(ieCapabilities);//
        driver.navigate().to("http://172.16.4.90:20125/BPS/");

        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        String loginwindow = driver.getWindowHandle();
        System.out.println(loginwindow);

        driver.findElement(By.xpath("/html/body/form/table[2]/tbody/tr/td[2]/table/tbody/tr[2]/td/table/tbody/tr[1]/td[3]/input")).clear();
        driver.findElement(By.xpath("/html/body/form/table[2]/tbody/tr/td[2]/table/tbody/tr[2]/td/table/tbody/tr[1]/td[3]/input")).sendKeys("WCHMOD_MAKER1");

        driver.findElement(By.xpath("//img[@src='images/indeximages/Login_button.gif']")).click();

        for (String winHandle : driver.getWindowHandles()) {

            ar.add(winHandle);
            System.out.println(winHandle);
        }

        driver.switchTo().window(ar.get(ar.size()-1));


        WebDriverWait wait = new WebDriverWait(driver, 10);
        WebElement element1 = wait.until(ExpectedConditions
                .visibilityOfElementLocated(By.xpath("//img[@src='images/wf/Logo2.jpg']")));

        driver.findElement(By.xpath("//div[@class=M5]")).click();

Exception faced : Unable to locate element on closed window

2
  • In your for loop, is it printing out the new Window?
    – Paul Muir
    Apr 11, 2016 at 16:59
  • Jigar, If you have resolved your issue, accept the answer. So the question will not be in active thread. Jul 27, 2016 at 9:06

3 Answers 3

1

Simple solution:

Set <String> handles =driver.getWindowHandles();
Iterator<String> it = handles.iterator();

String parent = it.next();
String child = it.next();

driver.switchTo().window(child);

//perform actions on child window
//perform actions on child window

driver.close(); // only for child wondow


driver.switchTo().window(parent);

//perform actions on parent window
//perform actions on parent window

driver.quit(); // After execution of main thread and for parent window

This works for me. Try your side!

0

From the error it looks like selenium is still searching for elements on the old window. Please make sure that line driver.switchTo().window(ar.get(ar.size()-1)); is actually switching to new window.

0

Try in try catch finally block as I used below it will work

status.selectByIndex(2);
                try {
                    clickElement(getSaveButton());
                } catch (NoSuchWindowException e) {
                    logger.info("Window got closed automatically by the system.");
                } finally {
                    getDriver().switchTo().window(workFlowWindow);
                }
            }
        }
        try {
            clickElement(getCloseButton());
        } catch (NoSuchWindowException e) {
            logger.info("Window got closed automatically by the system.");
        } finally {
            getDriver().switchTo().window(mainWindow);
        }
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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