Java Selenium:没有这样的窗口:目标窗口已关闭

Java Selenium:没有这样的窗口:目标窗口已关闭,java,selenium,selenium-chromedriver,Java,Selenium,Selenium Chromedriver,我想让我的脚本切换窗口。它应该切换到第二个窗口,然后再次切换回第一个窗口,以便我可以在当前窗口中继续操作。 我不明白为什么第一个窗口已经关闭了 public void doDetails(){ logger.info("########## Started ##########"); // Store the current window handle String winHandleBefore = driver.getWindowHan

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

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

    }

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

   //Switching to new window
    for(String winHandle : driver.getWindowHandles()){
      if(!(winHandle.equals(winHandleBefore)){
        driver.switchTo().window(winHandle);
       }
    }
这应该适合您。

…应该切换到第二个窗口,然后再次切换回第一个窗口。。。代码行是否在第二个窗口中执行?请查看此讨论