C# 为什么我不能使用SeleniumWebDriver通过Firefox下载文件?

C# 为什么我不能使用SeleniumWebDriver通过Firefox下载文件?,c#,webdriver,selenium-webdriver,C#,Webdriver,Selenium Webdriver,我试图使用配置文件设置在firefox中下载文件,但它不起作用。你能告诉我我做错了什么吗?我正在使用的代码发布在这行下面 var profile = new FirefoxProfile { EnableNativeEvents = true }; profile.SetPreference("browser.download.folderList", 2); profile.SetPreference("browser.download.manager.showWhenStarting", f

我试图使用配置文件设置在firefox中下载文件,但它不起作用。你能告诉我我做错了什么吗?我正在使用的代码发布在这行下面

var profile = new FirefoxProfile { EnableNativeEvents = true };
profile.SetPreference("browser.download.folderList", 2);
profile.SetPreference("browser.download.manager.showWhenStarting", false);
profile.SetPreference("browser.download.dir", folderName);
profile.SetPreference("browser.download.downloadDir", folderName);
profile.SetPreference("browser.download.defaultFolder", folderName);
profile.SetPreference("browser.helperApps.neverAsk.saveToDisk", "image/jpeg,application/vnd.oasis.opendocument.text,application/vnd.oasis.opendocument.spreadsheet," +
                                                                            "application/vnd.oasis.opendocument.presentation,application/vnd.oasis.opendocument.graphics," +
                                                                            "application/vnd.ms-excel,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet," +
                                                                            "application/vnd.ms-powerpoint,application/vnd.openxmlformats-officedocument.presentationml.presentation," +
                                                                            "application/vnd.openxmlformats-officedocument.wordprocessingml.document,application/vnd.mozilla.xul+xml," +
                                                                            "application/vnd.google-earth.kml+xml");

Selenium为每次运行创建一个新的firefox配置文件。您需要为selenium创建firefox配置文件,并让您的selenium脚本使用它。如果你在这个配置文件上设置了自动下载,那么它应该可以正常工作

看这里

我只为java做过这件事,但我想这个方法应该是类似的

编辑 用于指定配置文件的java代码为:

ProfilesIni profile = new ProfilesIni();
FirefoxProfile ffprofile = profile.getProfile("SELENIUM");
WebDriver driver = new FirefoxDriver(ffprofile);
资料来源:


在花了几天的时间尝试和阅读了大量的可能性之后,这本书对我很有用,因此我与你分享,我希望它能有用: 我只是这样设置webdriver firefox配置文件:

firefoxProfile.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/octet-stream doc xls pdf txt");

此解决方案允许我避免显示firefox下载弹出窗口,并且我可以使用selenium webdriver自动下载XLS文件。

使用以下设置使其正常工作:

FirefoxOptions options = new FirefoxOptions();
FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("browser.download.folderList", 2);
profile.setPreference("browser.download.dir", "C:\\Windows\\temp");
profile.setPreference("browser.download.useDownloadDir", true);
profile.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/octet-stream");
options.setProfile(profile);
driver = new FirefoxDriver(options);
非常重要的注意事项:应用程序/八位字节流首选项是在查看开发人员工具并观察文件的内容类型之后选择的。步骤:

开放式开发工具 上网 点击链接下载pdf 在网络面板中,选择第一个请求 mime类型是响应标头中的内容类型:
有关首选项设置的更多信息,请参见此处:。

那么,它不起作用了吗?它不会自动下载文件,我仍在使用文件下载窗口作为临时解决方法。我想您可以使用AutoIT脚本。您好,我不太熟悉AutoIT。您能给出一个示例脚本或告诉我从哪里开始吗?这将是非常有帮助的阅读:它每次都会创建一个新的空白配置文件,不是吗?我能让它记住我偏好的唯一方法是通过预定义的配置文件。