切换到链接打开的新浏览器窗口

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);// 回到原来页面

posted @ 2016-06-01 17:35  张申申  阅读(272)  评论(0编辑  收藏  举报