硒:无此窗口:目标窗口已关闭

时间:2019-01-16 08:42:25

标签: java selenium selenium-chromedriver

我希望我的脚本切换窗口。它应该切换到第二个窗口,然后再次切换回第一个,以便我可以在当前窗口中继续操作。 我找不到第一个窗口为什么已经关闭的原因。

   public void doDetails(){
        logger.info("########## Started ##########");

        // Store the current window handle
        String winHandleBefore = driver.getWindowHandle();
        getAllComponents();
        mouseOnArrow();
        WaitUtils.waitUntilVisiblityOfElement(driver,link,WaitUtils.FIVE_SECONDS);

        //Click operation opens another window/tab
        link.click();
        WaitUtils.waitForSeconds(WaitUtils.FIVE_SECONDS);

        //Switching to new window
        for(String winHandle : driver.getWindowHandles()){
            driver.switchTo().window(winHandle);
        }
        addFilter();
        WaitUtils.waitForSeconds(WaitUtils.TEN_SECONDS);
        components.clickCustomize();
        WaitUtils.waitUntilVisiblityOfElement(driver,components.allCheckboxes,WaitUtils.TWENTY_SECONDS);
        components.clickAllChkBoxes();
        WaitUtils.waitForSeconds(WaitUtils.ONE_SECOND);
        clickAllDetail();
        WaitUtils.waitForSeconds(WaitUtils.ONE_SECOND);
        components.clickApply();
        WaitUtils.waitUntilInvisibilityOf(driver,components.customizeHeader,WaitUtils.TWENTY_SECONDS);
        sortByAllowedAmount();
        WaitUtils.waitUntilInvisibilityOf(driver,components.loading,WaitUtils.TWENTY_SECONDS);
        clickExcel();
        WaitUtils.waitForSeconds(WaitUtils.TEN_SECONDS);

        //Closing current window
        driver.close();

        //Switching back to first window
        driver.switchTo().window(winHandleBefore);

    }

1 个答案:

答案 0 :(得分:1)

在代码中,您必须在切换到新窗口时添加检查。在您的代码中,您位于当前窗口。

   //Switching to new window
    for(String winHandle : driver.getWindowHandles()){
      if(!(winHandle.equals(winHandleBefore)){
        driver.switchTo().window(winHandle);
       }
    }

这应该对您有用。