Selenium Windows保存/打开对话框

Selenium Windows保存/打开对话框,selenium,Selenium,我正在尝试使用SeleniumWebDriver来自动化web应用程序。假设弹出下面的对话框,我的目标是自动单击保存文件,然后单击确定 Windows保存/打开文件下载Dialouge单独使用Selenium是不可能的 您必须将其与AutoIT结合使用: (或类似的东西) 我还想指出,在大多数情况下,您只需将文件路径直接“发送”(在特定语言绑定中使用“发送键”方法/函数)到上载控件,即可避免点击实际的浏览…按钮。一旦您这样做,Selenium的游戏就结束了。WebDriver无法直接与对话框窗

我正在尝试使用SeleniumWebDriver来自动化web应用程序。假设弹出下面的对话框,我的目标是自动单击保存文件,然后单击确定


Windows保存/打开文件下载Dialouge

单独使用Selenium是不可能的

您必须将其与AutoIT结合使用:

(或类似的东西)


我还想指出,在大多数情况下,您只需将文件路径直接“发送”(在特定语言绑定中使用“发送键”方法/函数)到上载控件,即可避免点击实际的
浏览…
按钮。一旦您这样做,Selenium的游戏就结束了。

WebDriver无法直接与对话框窗口交互这是因为对话框窗口是操作系统的域,而不是网页。但是,可以使用在对话框窗口上执行操作 名称空间System.Windows.Forms的SendKeys类方法SendWait()

using System.Windows.Forms;
在下面的示例代码中,按下一个PLUpload按钮,将打开一个Windows对话框来选择要上载的文件

以下行用于将键值发送到显示的对话框窗口

SendKeys.SendWait(@"C:\Users\Public\Pictures\Sample Pictures\Dock.jpg");
SendKeys.SendWait(@"{Enter}");
有关C#中SendKeys类的详细参考,请参见

//文件保存代码

FirefoxProfile profile =new FirefoxProfile();

profile.setPreferences("browser.download.folderlist",0);

profile.setpreferences("browser.helperApps.neverAsk.saveToDisk",mimeType);//applicatio/zip

WebDriver driver = new FireforDriver(profile);

driver.get("http:seleniumhq.org/download");

driver.get("http://selenium.com/files/selenium.zip");
//文件打开代码

profile.setPreferences("browser.helperApps.neverAsk.open",mimeType);

使用此方法进行文件处理:

我们需要:

jacob.jar

它将包含一个jar文件和2个.dll文件

AutoItX4Java.jar


谢谢。。。不要单击“接受”或“向上投票”,直到它对您有效。如果它不适用于您的意思,请评论。。不要投反对票…

谢谢Arran。我刚在谷歌上找到这个链接。它有一个汽车的示例代码avehttp://www.autoitscript.com/wiki/Snippets_(文件%26文件夹)#文件打开.2FSave.2folder对话框
profile.setPreferences("browser.helperApps.neverAsk.open",mimeType);
public static void uploadFile(String path, String browser){

if(browser.equalsIgnoreCase("chrome")){

    if(x.winWaitActive("Open", "", 10)){
        if(x.winExists("Open")){
            x.sleep(500);
            x.send(path);
            x.controlClick("Open", "", "Button2");

        }
    }

}


if(browser.equalsIgnoreCase("firefox")){

    if(x.winWaitActive("File Upload", "", 10)){
        if(x.winExists("File Upload")){
            x.sleep(500);
            x.send(path);
            x.controlClick("File Upload", "", "Button2");

        }
    }
}

if(browser.equalsIgnoreCase("InternetExplorer")){

    if(x.winWaitActive("Choose File to Upload", "", 10)){
        if(x.winExists("Choose File to Upload")){
            x.sleep(500);
            x.send(path);
            x.controlClick("Choose File to Upload", "", "Button2");

        }
    }
}



}


public void test(){
    //Click on the Select button of the file upload
    uploadFile("Path", "chrome");


}