用Java或Javascript处理窗口对话框和弹出窗口

我需要使用基于Java或Javascript的自动化解决方案来操作IE浏览器的弹出窗口和下载对话框。

我尝试了selenium2,但它没有正常工作所以任何其他建议相同。 实际上selenium2没有正确处理警报/下载对话框,所以我想使用其他一些javascript / java解决方案。

使用下载对话框:我需要将下载的文件保存到特定位置。 使用警报对话框:我需要检查显示的消息并单击特定按钮。

任何建议表示赞赏。 谢谢。

我使用selenium 1,它可以很好地处理我的应用程序中的弹出窗口。

//Click on browse file button, open a popup selenium.click("//input[@value='Browse...']"); //waiting for popup to load selenium.waitForPopUp("_Dialog", "30000"); //selecting the popup by passing window name selenium.selectWindow("name=_Dialog"); //click a link inside pop up window selenium.click("link=something"); //Put other popup operations here //click cancel button for pop up selenium.click("cancel"); //back to main window selenium.selectwindow("null") 

要从警告框中获取消息,请使用selenium.getAlert(); 。 这将以警报框的forms返回包含在消息框中的消息。

此外,有时您需要检查是否在切换之前已发生警报。

  int noofWindows = selenium.getAllWindowNames().length; if (noofWindows > 1){ //selects the second window selenium.selectWindow(selenium.getAllWindowIds()[2]); //Prints the message in the alert window System.out.println(selenium.getAlert()); } 

如果不需要在IE中运行测试,请在执行代码之前使用firefox(* chrome)并关闭所有其他窗口。

我希望这可以帮助你。

*所有提到的代码都用于处理JavaScript弹出窗口。 我不确定这是否适用于Vb脚本。

编辑

我认为IE下载弹出窗口是一个windows事件所以不能直接由selenium处理,为此你必须使用Java AWT或AutoIT。

AutoIT脚本应该是类似的东西

 WinWaitActive(windowTitle) ControlClick(windowTitle,"",buttonName) 

并将其另存为IEsave.exe。 注意:我没有尝试过这个AutoIT脚本。

现在您已从程序中执行IEsave.exe。 我在这里使用java。

 java.lang.Runtime.getRuntime().exec("c:/IEsave.exe"); 

这将执行文件,该文件又将处理windows的保存按钮事件。

您可以创建类似的exe文件来处理其他窗口的事件。

希望这能解决你的问题。