【问题标题】:Selenium C# - Chrome Driver doesn't download files on headless modeSelenium C# - Chrome 驱动程序不会在无头模式下下载文件
【发布时间】:2020-02-04 19:57:38
【问题描述】:

我正在使用第 77 版 chrome 来测试一些下载。但我不明白为什么它不允许在无头模式下下载文件(仅在无头模式下发生)。这是我正在使用的代码。

_chromeOptions.AddUserProfilePreference("download.default_directory", @"目录文件夹"); _chromeOptions.AddUserProfilePreference("intl.accept_languages", "nl"); _chromeOptions.AddUserProfilePreference("disable-popup-blocking", "true"); _webdriver = new ChromeDriver(_chromeOptions);

【问题讨论】:

    标签: c# selenium automated-tests selenium-chromedriver


    【解决方案1】:

    您也可以使用 Firefox 无头浏览器下载文件。

    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.manager.showWhenStarting", false );
    profile.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/octet-stream");
    options.setProfile(profile);
    driver = new FirefoxDriver(options);
    

    【讨论】:

    • 如果你坚持使用chrome,看看这个。 stackoverflow.com/a/33435817/10515117
    • 是的,我正在使用这些解决方案,但仍然不让我使用。我必须在 chrome 上下载它是强制性的
    【解决方案2】:

    默认情况下,Chrome Headless 模式下会禁用文件下载。见:https://bugs.chromium.org/p/chromium/issues/detail?id=696481

    您需要对驱动程序进行 API 调用以启用它。

    var driver = new ChromeDriver(driverService, options);
    // Allow download in headless mode
    var param = new Dictionary<string, string> {{"behavior", "allow"}, {"downloadPath", DownloadPath}
    };
    var cmdParam = new Dictionary<string, object> {{"cmd", "Page.setDownloadBehavior"}, {"params", param}};
    var url = driverService.ServiceUrl + "session/" + driver.SessionId + "/chromium/send_command";
    var cli = new WebClient {Headers = {[HttpRequestHeader.ContentType] = "application/json"}};
    _ = cli.UploadString(url, JsonConvert.SerializeObject(cmdParam));
    

    【讨论】:

      【解决方案3】:

      此函数返回自动下载设置为“USERPROFILE”下载文件夹的无头 Chrome 浏览器实例。你可以硬编码你想要的下载文件夹。

      从测试初始化​​程序调用函数 GetBrowserWebDriver("Chrome")

      public IWebDriver GetBrowserWebDriver(string browser)
              {
                  IWebDriver currentDriver = null;        
                  switch (browser)
                  {
                      case "Chrome":                    
                          var options = new ChromeOptions();
                          options.AddArgument("headless");
                          string downloadPath = System.Environment.GetEnvironmentVariable("USERPROFILE") + "\\Downloads";
                          options.AddUserProfilePreference("download.default_directory", downloadPath);
                          options.AddUserProfilePreference("profile.default_content_setting_values.automatic_downloads", 1);
                          options.AddArgument("--window-size=1920,1080");                    
                          currentDriver = new ChromeDriver(options);
                          break;
                      case "Firefox":
                          currentDriver = new FirefoxDriver();
                          break;
                      case "IE":
                          currentDriver = new InternetExplorerDriver(new InternetExplorerOptions() { IgnoreZoomLevel = true });
                          break;
                      default:
                          throw new NotSupportedException("");
                  }
                  return currentDriver;
              }
      

      【讨论】:

        【解决方案4】:

        我能够使用以下 ChromeOptions 以无头模式下载文件:

                    var chromeOptions = new ChromeOptions();
                    chromeOptions.AddArgument("--headless");
                    chromeOptions.AddArgument("--disable-gpu");
                    chromeOptions.AddUserProfilePreference("download.default_directory", ApplicationSettings.StagingDirectory);
                    chromeOptions.AddUserProfilePreference("profile.default_content_setting_values.automatic_downloads", 1);
                    driver = new ChromeDriver(chromeOptions);
        

        Chrome 版本 - 89.0.4389

        Chrome 驱动程序版本 - 89.0.4389.2300

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2022-06-22
          • 2023-02-10
          • 1970-01-01
          • 2023-03-19
          • 2019-12-04
          • 2020-11-30
          • 2023-03-10
          相关资源
          最近更新 更多