selenium WebDriver之切换到链接打开的新浏览器窗口
selenium WebDriver测试网页时,点击target=”_blank”的链接,在打开新页面,切换到新窗口的方法如下:
@Test
public void testYpd() throws Exception {
driver.get(baseUrl + “/”);
driver.findElement(By.linkText(“6san.com”)).click();
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());//切换到新窗口
System.out.println(“New page title is:” + window.getTitle());
window.close();//关闭新窗口
}
driver.switchTo().window(currentWindow);//回到原来页面
}