public void swithWindowTest() {
driver.findElement(By.className("open")).click();
String winHandleBefore = driver.getWindowHandle(); // 获取当前窗口的句柄
for (String winHandle : driver.getWindowHandles()) { // 遍历所有窗口的句柄
if (driver.switchTo().window(winHandle).getTitle().contains("百度")) {
driver.switchTo().window(winHandle); // 切换到title中包含“百度”的窗口中
break;
}
}
driver.findElement(By.id("kw")).sendKeys("selenium baidu"); // 输入selenium baidu
driver.findElement(By.id("su")).click();
driver.switchTo().window(winHandleBefore); // 切换到之前的窗口
driver.findElement(By.id("user")).sendKeys("selenium Demo"); // 输入selenium demo
}
分享标题:seleniumswitchTo()切换窗口
路径分享:
http://kswsj.cn/article/gjicie.html