Selenium C#-Chrome驱动程序不会以无头模式下载文件 [英] Selenium C# - Chrome Driver doesn't download files on headless mode

查看:352
本文介绍了Selenium C#-Chrome驱动程序不会以无头模式下载文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm using the 77th version of chrome to test some downloads. But I don't understand why it doesn't let download files on headless mode (Only happens on headless mode). This is the code I'm using.

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

_chromeOptions.AddUserProfilePreference("download.default_directory", @"Directory Folder"); _chromeOptions.AddUserProfilePreference("intl.accept_languages", "nl"); _chromeOptions.AddUserProfilePreference("disable-popup-blocking", "true"); _webdriver = new ChromeDriver(_chromeOptions);

推荐答案

默认情况下,Chrome无头模式禁用文件下载.请参阅: https://bugs.chromium.org/p/chromium/issues/detail?id = 696481

Downloading of files is disabled in Chrome Headless mode by default. See: https://bugs.chromium.org/p/chromium/issues/detail?id=696481

您需要对驱动程序进行API调用才能启用它.

You need to make an API call to the driver to enable it.

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

这篇关于Selenium C#-Chrome驱动程序不会以无头模式下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆