窗口对话框在Java或Javascript中的弹出处理

Window Dialogs & Popup handling in Java or Javascript

本文关键字:处理 Javascript 对话框 Java 窗口      更新时间:2023-09-26

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

我尝试了硒2,但它不能正常工作,所以任何其他建议相同。实际上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();。这将以String形式返回警报框中包含的消息。

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

        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-script。

编辑

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

AutoIT脚本应该类似于
WinWaitActive(windowTitle)
ControlClick(windowTitle,"",buttonName)

并保存为IEsave.exe。注意:我还没有试过这个自动it脚本。

现在你已经从你的程序中执行了IEsave.exe。我这里用的是java。

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

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

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

希望这能解决你的问题