Html代码<span>test.html</span><html> <head><title>TestPopupWindow</title></head> <body> <a id = "51" href ="http://www.51.com/" target = "_blank">Let'sgo!</a> </body></html>
![[转载]WebDriver拾级而上·之六获得弹出窗口 webdriver拾级而上](http://img.413yy.cn/images/31101031/31082932t01d5c7d613e3558c6e.jpg)
下面的代码演示了如何去得到弹出的新窗口Java代码
import java.util.Iterator;import java.util.Set;
import org.openqa.selenium.By;import org.openqa.selenium.WebDriver;import org.openqa.selenium.firefox.FirefoxDriver;
public class PopupWindowTest {public static void main(String[] args) {System.setProperty("webdriver.firefox.bin","D:ProgramFilesMozilla Firefoxfirefox.exe"); WebDriver dr = new FirefoxDriver();String url ="YourPathtomain.html";dr.get(url);dr.findElement(By.id("51")).click();
//得到当前窗口的句柄String currentWindow = dr.getWindowHandle();//得到所有窗口的句柄Set<String> handles =dr.getWindowHandles();
Iterator<String> it =handles.iterator();while(it.hasNext()){if(currentWindow == it.next())continue;WebDriver window =dr.switchTo().window(it.next());System.out.println("title,url ="+window.getTitle()+","+window.getCurrentUrl());}}
}
输出结果:title,url = 51.com 真人配对玩游戏,http://www.51.com/