我正在尝试以chrome headless模式下载pdf,但不能。
我尝试了下面的代码。
我也尝试在选项中传递Page.setDownloadBehavior
。
static async Task AllowHeadlessDownload(ChromeDriverService
driverService, ChromeDriver driver)
{
var jsonContent = new JObject(
new JProperty("cmd", "Page.setDownloadBehavior"),
new JProperty("params",
new JObject(new JObject(
new JProperty("behavior", "allow"),
new JProperty("downloadPath", @"D:\pdf")))));
var content = new StringContent(jsonContent.ToString(),
Encoding.UTF8, "application/json");
var sessionIdProperty =
typeof(ChromeDriver).GetProperty("SessionId");
var sessionId = sessionIdProperty.GetValue(driver, null) as
SessionId;
using (var client = new HttpClient())
{
client.BaseAddress = driverService.ServiceUrl;
var result = await client.PostAsync("session/" +
sessionId.ToString() + "/chromium/send_command", content);
var resultContent = await
result.Content.ReadAsStringAsync();
}
}
public ChromeDriver ChromeHeadless()
{
var options = new ChromeOptions();
options.AddArguments("headless");
options.AddArguments("--window-size=1920,1080");
driverService = ChromeDriverService.CreateDefaultService();
ChromeDriver driver = new ChromeDriver(options);
Task.Run(() => AllowHeadlessDownload(driverService,
driver));
return driver;
}