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

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

Downloading Files with Headless Chrome - Selenium C#

问题

 ChromeOptions options = new ChromeOptions();
           
            var chromeDriverService = ChromeDriverService.CreateDefaultService();
             chromeDriverService.HideCommandPromptWindow = true;
             chromeDriverService.SuppressInitialDiagnosticInformation = true;
           
            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");
英文:
 ChromeOptions options = new ChromeOptions();
           
            var chromeDriverService = ChromeDriverService.CreateDefaultService();
             chromeDriverService.HideCommandPromptWindow = true;
             chromeDriverService.SuppressInitialDiagnosticInformation = true;
           
            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");

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

以下是代码部分的翻译:

我的使用情况略有不同。不过,下面的代码为我提供了预期的结果。

**参考以下代码:**

ChromeOptions options = new ChromeOptions();
options.AddUserProfilePreference("download.default_directory", @"C:\Downloads");
options.AddUserProfilePreference("download.prompt_for_download", false);
ChromeDriver driver = new ChromeDriver(options);
driver.Navigate().GoToUrl("https://example.com/download-file");
IWebElement downloadLink = driver.FindElement(By.Id("download-link"));
downloadLink.Click();
System.Threading.Thread.Sleep(4000); // 等待4秒钟
driver.Close();
英文:

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

Refer to the below code :

ChromeOptions options = new ChromeOptions();
options.AddUserProfilePreference("download.default_directory", @"C:\Downloads");
options.AddUserProfilePreference("download.prompt_for_download", false);
ChromeDriver driver = new ChromeDriver(options);
driver.Navigate().GoToUrl("https://example.com/download-file");
IWebElement downloadLink = driver.FindElement(By.Id("download-link"));
downloadLink.Click();
System.Threading.Thread.Sleep(4000); // wait for awhile for 4 seconds
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:

确定