Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Save As PDF Chrome using selenium

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.

like image 985
user2580925 Avatar asked Jul 26 '17 11:07

user2580925


People also ask

Can we automate PDF using selenium?

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.

Why can't I save as PDF in Chrome?

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.

How does selenium work with PDF?

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.


2 Answers

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.

like image 163
Elliot Avatar answered Oct 14 '22 04:10

Elliot


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);
like image 36
jadupl Avatar answered Oct 14 '22 03:10

jadupl