1 Set<String> windows = browser.getWebDriver().getWindowHandles(); //获得所有窗口句柄 2 for (String string : windows) { 3 System.out.println(string); //打印所有窗口句柄 4 } 5 String currWin = browser.getWebDriver().getWindowHandle(); 6 System.out.println(currWin); 7 Iterator<String> it = windows.iterator(); 8 while(it.hasNext()) { 9 String handle = it.next(); 10 if(currWin.equals(handle)) 11 continue; 12 browser.getWebDriver().switchTo().window(handle); //切换窗口 13 } 14 15 16 browser.pause(5); 17 18 String Win2 = browser.getWebDriver().getWindowHandle(); 19 System.out.println(Win2); //再次打印当前窗口句柄,与currWin不一样