使用Selenium和Chrome保存为PDF

ff29svar  于 2022-11-24  发布在  其他
关注(0)|答案(1)|浏览(395)

我试图使用打印对话框保存为PDF,我已经使用Selenium和Headless Chrome(v81)登录了这个网页。所有的文章都说我应该能够使用kiosk模式打印/保存文档为PDF,在这种模式下,打印到PDF会自动发生,所以预览对话框会被抑制。
I cannot get Chrome to default to saving as a PDF when using SeleniumSelenium 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命令行,但事实上我需要登录到网站。上面的代码有什么问题?

nnt7mjpx

nnt7mjpx1#

2022年5月20日更新:有了Selenium 4,现在可以更容易地“保存到PDF”。您的应用程序需要在 *headless模式 * 下运行,如果不是,Selenium将抛出异常PrintToPDF is only supported in headless mode。但是如果由于某种原因,您 * 不能 * 在headless模式下运行,我之前的回复仍然有效。

using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using System;
using System.IO;

private static ChromeOptions _chromeOptions;
private static WebDriver _driver;
private static string fileName = "MyPrint";
private static string printFinalPath = Path.Combine($@"C:\Users\{Environment.UserName}\Downloads", string.Concat(fileName, ".pdf"));

static void Main(string[] args)
{
     _chromeOptions = new ChromeOptions();
     _chromeOptions.AddArguments("headless"); //your chrome driver needs run in headless!

     //creating webdriver
     _driver = new ChromeDriver(_chromeOptions);

     //go to the website
     _driver.Navigate().GoToUrl("https://www.google.com/");

     //defining configurations for de printing
     PrintOptions printOptions = new PrintOptions
     {
          Orientation = PrintOrientation.Portrait
     };

     //printing...
     PrintDocument printDocument = _driver.Print(printOptions);

     //saving the file
     printDocument.SaveAsFile(printFinalPath);
}

原始回复Selenium 4有一些新的特性,比如,通过ChromeDevTools的“Executing Command“。有了这个特性,我们可以直接从chrome调用“PdfAsync”之类的东西(像Puppersharp,但是使用了selenium)。但是不幸的是,我们还没有在c#中启用这个选项。“Print-pdf”在将来会更容易。

但是,使用Selenium 3.141.0,从Web上搜索一些主题,我用你的代码解决了这个问题。我有一些问题,转换字符串C#到json对象。
在chrome中使用Logging,以提供帮助。

2021年7月27日更新:不幸的是,使用参数“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!
}

如果“另保存为”窗口仍然打开,则某些参数不正确。

相关问题