使用无头Chrome下载文件 – Selenium C#

huangapple go评论111阅读模式
英文:

Downloading Files with Headless Chrome - Selenium C#

问题

  1. ChromeOptions options = new ChromeOptions();
  2. var chromeDriverService = ChromeDriverService.CreateDefaultService();
  3. chromeDriverService.HideCommandPromptWindow = true;
  4. chromeDriverService.SuppressInitialDiagnosticInformation = true;
  5. options.AddArgument("--headless");
  6. string downloadPath = System.Environment.GetEnvironmentVariable("USERPROFILE") + "\\Downloads";
  7. options.AddUserProfilePreference("download.default_directory", downloadPath);
  8. options.AddUserProfilePreference("profile.default_content_setting_values.automatic_downloads", 1);
  9. options.AddArgument("--window-size=1920,1080");
英文:
  1. ChromeOptions options = new ChromeOptions();
  2. var chromeDriverService = ChromeDriverService.CreateDefaultService();
  3. chromeDriverService.HideCommandPromptWindow = true;
  4. chromeDriverService.SuppressInitialDiagnosticInformation = true;
  5. options.AddArgument("--headless");
  6. string downloadPath = System.Environment.GetEnvironmentVariable("USERPROFILE") + "\\Downloads";
  7. options.AddUserProfilePreference("download.default_directory", downloadPath);
  8. options.AddUserProfilePreference("profile.default_content_setting_values.automatic_downloads", 1);
  9. options.AddArgument("--window-size=1920,1080");

For some reason I cannot download files in chrome when running headless in Selenium -

When not running in headless mode there is no issue downloading files.

Selenium Webdriver Chromedriver V110.0.5

答案1

得分: 7

根据 Selenium 开发页面:https://www.selenium.dev/blog/2023/headless-is-going-away/

你现在需要使用 "--headless=new"

然而,即使使用这个新属性,我仍然在下载文件方面遇到困难。

英文:

According to Selenium dev page: https://www.selenium.dev/blog/2023/headless-is-going-away/

you need to user "--headless=new" from now.

However, I'm still struggling with downloading files, even using this new attribute

答案2

得分: 0

以下是代码部分的翻译:

  1. 我的使用情况略有不同。不过,下面的代码为我提供了预期的结果。
  2. **参考以下代码:**
  3. ChromeOptions options = new ChromeOptions();
  4. options.AddUserProfilePreference("download.default_directory", @"C:\Downloads");
  5. options.AddUserProfilePreference("download.prompt_for_download", false);
  6. ChromeDriver driver = new ChromeDriver(options);
  7. driver.Navigate().GoToUrl("https://example.com/download-file");
  8. IWebElement downloadLink = driver.FindElement(By.Id("download-link"));
  9. downloadLink.Click();
  10. System.Threading.Thread.Sleep(4000); // 等待4秒钟
  11. driver.Close();
英文:

My use case was a bit different. However, the below code gave me the intended results.

Refer to the below code :

  1. ChromeOptions options = new ChromeOptions();
  2. options.AddUserProfilePreference("download.default_directory", @"C:\Downloads");
  3. options.AddUserProfilePreference("download.prompt_for_download", false);
  4. ChromeDriver driver = new ChromeDriver(options);
  5. driver.Navigate().GoToUrl("https://example.com/download-file");
  6. IWebElement downloadLink = driver.FindElement(By.Id("download-link"));
  7. downloadLink.Click();
  8. System.Threading.Thread.Sleep(4000); // wait for awhile for 4 seconds
  9. driver.Close();

huangapple
  • 本文由 发表于 2023年2月16日 03:50:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/75464809.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定