自动设置WebDriver的configuration文件来打开Excel文件
我find了解决scheme来悄悄下载文件(没有打开/保存对话框),但找不到任何设置WebDriverconfiguration文件自动打开一个文件而不保存。
使用selenium和C#。 该文件是一个.csv,这里是我的代码:
FirefoxProfile profile = new FirefoxProfile(); //set to automatically open file profile.SetPreference("browser.helperApps.alwaysAsk.force", false); profile.SetPreference("browser.helperApps.neverAsk.openFile","text/csv,application/vnd.ms-excel,application/excel"); profile.SetPreference("browser.download.manager.showWhenStarting", false); Instance = new FirefoxDriver(profile);
我是否缺less插件?
我通过安装FF扩展InlinedDisposition 2来解决这个问题,下面是更新后的代码:
FirefoxProfile profile = new FirefoxProfile(); try { profile.AddExtension(@"E:\Firefox Extensions\InlineDisposition2.xpi"); } catch (IOException e) { throw new FileNotFoundException("Could not load required extensions, did you download them to the above location? ", e.Message); } //set to automatically open excel file profile.SetPreference("browser.helperApps.alwaysAsk.force", false); profile.SetPreference("browser.helperApps.neverAsk.openFile", "application/octectstream,text/x-csv,text/csv,application/vnd.ms-excel"); Instance = new FirefoxDriver(profile);
我相信这背后的原因是,有时网站发送有关文件的错误信息。 而且,如果服务器用通用的内容types发送文件,那么它将不起作用。
这个插件处理这个问题。
我希望这有帮助。