String currentWindow = driver.getWindowHandle();// 获取当前窗口句柄
Set<String> handles = driver.getWindowHandles();// 获取所有窗口句柄 Iterator<String> it = handles.iterator(); while (it.hasNext()) { if (currentWindow == it.next()) { continue; } WebDriver window = driver.switchTo().window(it.next());// 切换到新窗口 if (window.getTitle().equals(Constant.jd_login_title)) { WebElement loginname = window.findElement(By.name("loginname")); loginname.click(); loginname.sendKeys(user.getName()); WebElement nloginpwd = window.findElement(By.name("nloginpwd")); nloginpwd.click(); nloginpwd.sendKeys(user.getPwd()); // by classname的用法 window.findElement(By.id("loginsubmit")).click(); } // window.close();// 关闭新窗口 } // driver.switchTo().window(currentWindow);// 回到原来页面