一般来说,操作都是在由 Selenium 启动的主窗口中执行。如果您想在一个由 window.open
函数生成的新窗口中执行操作,那么将焦点更换到新窗口。在弹出窗口中执行操作之后,焦点返回到主窗口。处理弹出窗口的过程定义在清单 1中。
//wait for the popup window with timeout; selenium.waitForPopUp(windowname, timeout); //select the pop up window selenium.selectWindow(popupWindowIdentifier); //perform action on popup window and close the window; .... //return to the main window use 'null' selenium.selectWindow(null); |
windowname
是调用 window.open
函数的第二个参数。上面提到的 popupwindowIdentifier
是一个窗口标识符,可以是窗口 ID、窗口名称、title=the title of the window
或 var=javascript variable
。如果弹出窗口的属性未知,但是真的定义了,那么使用 getAllWindowIds()
、getAllWindowNames()
或 getAttributeFromAllWindows()
函数来检索弹出窗口的属性。
在最新版的 Selenium RC 1.0.1 中,Selenium 添加了像 selectPopUp(String)
和 deselectPopUp()
这样的方法,它们的功能在以前版本中由 selectWindow(String)
提供。
//wait for the popup window with timeout; selenium.waitForPopUp(“”, timeout); //same as selenium.selectWindow selenium.selectPopUp(“”); //perform action on popup window and close the window; .... //same as selenium.selectWindow(null); selenium.deselectPopUp(); |
清单 2展示了处理弹出窗口最简单的方式。您可以保留 waitForPopUp
和 selectPopUp
函数中的第一个参数为空。如果同时弹出多个窗口,请指定窗口属性。