I am looking to automate "Save as PDF" of Chrome using selenium. AFAIK it is not supported by the Selenium out of the box. Therefore, i am trying to write my own. I am having an issue. Actually clicking on the Print button in my webpage opens a new window with printable area. I am trying to switch to this window using SwitchTo. But it is timing out always.
Selenium does not have any inbuilt functionality to test the content of PDF files; hence it needs to use the third-party library Apache PDFBox. It is an open-source Java tool and can be used with Selenium Java and TestNG to assert the content of PDF.
If it still does not work, then right-click to reveal the "Save as" option to save the PDF. If you still cannot see the "Save as", then you need to disable the PDF Viewer and revert to Adobe Reader. By doing this, type "about plugins"(without the quotes) in the address bar in Chrome and press Enter.
To handle a PDF document in Selenium test automation, we can use a java library called PDFBox. Apache PDFBox is an open-source library that exclusively helps in handling the PDF documents. We can use it to verify the text present in the document, extract a specific section of text or image in the documents, and so on.
You can add the options.AddArgument("--kiosk-printing"); to have it automatically click the print button.
That is working for me but I have a problem setting the printer to Save as PDF. It is printing to the printer instead.
You could try to disable the Chrome PDF plugin and download promt window with desired capabilities. Something like this:
DesiredCapabilities cap = DesiredCapabilities.chrome();
cap.setCapability("download.default_directory","C:");
cap.setCapability("download.prompt_for_download","false");
cap.setCapability("directory_upgrade","true");
cap.setCapability("plugins.plugins_disabled","Chrome PDF Viewer");
WebDriver driver = new ChromeDriver(cap);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With