【问题标题】:Setup WebDriver's profile automatically to open Excel files自动设置 WebDriver 的配置文件以打开 Excel 文件
【发布时间】:2015-10-07 17:12:11
【问题描述】:

我找到了静默下载文件的解决方案(没有打开/保存对话框),但找不到任何用于设置 WebDriver 配置文件自动打开文件而不保存的内容。

使用 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);

我是否缺少插件的某些内容?

【问题讨论】:

    标签: c# excel selenium selenium-firefoxdriver


    【解决方案1】:

    我通过安装 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);
    

    我认为这背后的原因是网站有时会发送有关文件的错误信息。而且,如果服务器发送具有通用内容类型的文件,那么它将无法正常工作。

    那个插件解决了这个问题。

    我希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-06-14
      • 2012-08-29
      • 2018-04-20
      • 2022-11-10
      • 1970-01-01
      • 2020-10-24
      • 1970-01-01
      相关资源
      最近更新 更多