Selenium: 处理弹出窗口的workaround

在Selenium中,比较麻烦的是处理弹出窗口,虽然有selectWindow(String windowID)的方法可用,但是对于某些窗口很难拿到windowID的话,比如开发人员没在HTML中提供ID,或者getAllWindowIds()得不到弹出窗口的ID的时候,这个方法基本等于没用。

在这个时候我们可以使用一个workaround:

openWindow(String url, String windowID) :Opens a popup window (if a window with that ID isn't already open). After opening the window, you'll need to select it using the selectWindow command.

比如对于一段HTML代码:<a target="newwindow" href="relative_URL">click here</a>

可以使用xpath得到href的属性:a[text()='click here']@href 

String url = selenium.getAttribute("a[text()='click here']@href "); 

然后再加上server的地址,就得到弹出子窗口的地址 

然后使用OpenWindow(URL, ID)之后

waitForPopUp(ID, delaytime);

selectWindow(ID);

就选中了子窗口,然后可以在上面进行操作,比如验证文字存在,输入数据之类的

posted @ 2010-09-27 13:25  小楼  阅读(3631)  评论(0编辑  收藏  举报