java.util.NoSuchElementException解决办法

报错代码

public void switchToNewWindow(){
    //得到当前句柄
String currentWindow = driver.getWindowHandle();
    //得到所有窗口的句柄
    Set<String> handles = driver.getWindowHandles();
     
    //排除当前窗口的句柄,则剩下是新窗口
    Iterator<String> it = handles.iterator();
    while(it.hasNext()){
        if(currentWindow == it.next())  continue;
        driver.switchTo().window(it.next());      
    }
     
}

修改后的代码

    String currentWindow = driver.getWindowHandle();
          //得到所有窗口的句柄
          Set <String> handles = driver.getWindowHandles(); 
          //排除当前窗口的句柄,则剩下是新窗口
          Iterator<String> it = handles.iterator();
          while(it.hasNext()){
              String it1 = it.next();
              if(currentWindow == it1)  continue;
              driver.switchTo().window(it1);      
          } 

参考博客 http://www.cnblogs.com/joewu/p/3718721.html

原文地址:https://www.cnblogs.com/tsw123/p/4834334.html