我正在尝试使用打印对话框另存为pdf,这是一个我已经使用Selenium和无头铬(v81)登录的网页。所有文章声明我应该能够使用kiosk模式将文档打印/保存为pdf,其中打印到pdf会自动发生,因此预览对话框被抑制,例如
I cannot get Chrome to default to saving as a PDF when using Selenium Selenium Chrome save as pdf change download folder
到目前为止,我有以下代码:
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.AddArguments("--headless", "--disable-infobars", "--disable-extensions", "--test-type", "--allow-insecure-localhost", "--ignore-certificate-errors", "--ignore-ssl-errors=yes", "--disable-gpu", "--kiosk-printing", "--allow-running-insecure-content");
chromeOptions.AcceptInsecureCertificates = acceptInsecureCertificates;
chromeOptions.UnhandledPromptBehavior = UnhandledPromptBehavior.Ignore;
chromeOptions.AddUserProfilePreference("print.always_print_silent", true);
chromeOptions.AddUserProfilePreference("download.default_directory", @"C:\Temp");
chromeOptions.AddUserProfilePreference("savefile.default_directory", @"C:\Temp");
chromeOptions.AddUserProfilePreference("download.prompt_for_download", false);
chromeOptions.AddUserProfilePreference("printing.default_destination_selection_rules", "{\"kind\": \"local\", \"namePattern\": \"Save as PDF\"}");
chromeOptions.AddUserProfilePreference("printing.print_preview_sticky_settings.appState", "{\"recentDestinations\": [{\"id\": \"Save as PDF\", \"origin\": \"local\", \"account\": \"\" }],\"version\":2,\"isGcpPromoDismissed\":false,\"selectedDestinationId\":\"Save as PDF\"}");
webDriver = new ChromeDriver(ChromeDriverService.CreateDefaultService(), chromeOptions, TimeSpan.FromMinutes(timeoutMinutes));然后我使用下面的代码打印页面:
var driver = FeatureContext.Current.GetWebDriver();
driver.ExecuteJavaScript("window.print();");但我看到打印预览对话框,总是和我的默认打印机设置,而不是另存为PDF。
但是,我会使用chrome命令行,因为我需要登录到该站点。上面的代码有什么问题?
发布于 2021-07-27 05:06:54
Selenium 4有一些新特性,比如通过ChromeDevTools实现的"Executing Command“。有了这个特性,我们可以直接从chrome调用类似"PdfAsync“的东西(像Puppersharp,但是使用selenium)。但不幸的是,我们还没有在c#中启用此选项。“Print-pdf”在未来会更容易。
然而,使用selenium 3.141.0,在web主题中做了一些搜索,我在您的代码的帮助下解决了这个问题。我在将字符串C#转换为json对象时遇到了一些问题。
使用chrome中的Logging来提供帮助。
更新27/07/2021:不幸的是,使用参数"headless“的"window.print()”将不起作用。
我的最终代码是:
public class Program
...
static void Main(string[] args)
{
private static IWebDriver _driver;
private static ChromeOptions _chromeOptions;
private static ChromeDriverService _chromeService;
private static string pathLogChromeDriver = "chrome_driver_logs";
private static string nameFileLog = "chromedriver.log";
//creating chrome service, to logging
_chromeService = ChromeDriverService.CreateDefaultService(".");
//using to see the modifications in google chrome arguments
_chromeService.LogPath = Path.Combine(Directory.GetCurrentDirectory(), pathLogChromeDriver, nameFileLog);
_chromeService.EnableVerboseLogging = true;
//creating chromeOptions, to set the profile preferences
_chromeOptions = new ChromeOptions();
//using Dictionary to pass parameter name (string) and json object to AddUserProfilePreference.
Dictionary<string, object> prefs = new Dictionary<string, object>();
prefs.Add("downloadFile", "{\"recentDestinations\": [{\"id\": \"Save as PDF\", \"origin\": \"local\", \"account\": \"\" }],\"version\":2,\"isGcpPromoDismissed\":false,\"selectedDestinationId\":\"Save as PDF\"}");
prefs.Add("pathFinalFile", "C:\\Users\\" + Environment.UserName + "\\Downloads\\");
//using the dictionary values to set the parameters
_chromeOptions.AddUserProfilePreference("printing.print_preview_sticky_settings.appState", prefs["downloadFile"])
_chromeOptions.AddUserProfilePreference("savefile.default_directory", prefs["pathFinalFile"]);
//disable 'chrome is being controlled by automated test software' bar
_chromeOptions.AddUserProfilePreference("useAutomationExtension", false);
_chromeOptions.AddExcludedArgument("enable-automation");
//using --kiosk-printing to enable "silent printing"
_chromeOptions.AddArgument("--kiosk-printing");
//creating webdriver
_driver = new ChromeDriver(_chromeService, _chromeOptions);
_driver.Navigate().GoToUrl("https://applitools.com/blog/selenium-4-chrome-devtools/");
_driver.Manage().Window.Maximize();
//run javascript to print
_driver.ExecuteJavaScript("window.print();");
//is done!
}如果窗口“另存为”仍然打开,则说明某些参数不正确。
https://stackoverflow.com/questions/61798725
复制相似问题