代码改变世界

switch窗口句柄

2018-04-17 11:34  钱先生  阅读(315)  评论(0编辑  收藏  举报
 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不一样