How to save as PDF on Chrome using Selenium

0 votes

I am trying to automate "Save as PDF" of Chrome using selenium. As far as I know, It is not supported by Selenium. Therefore, I am trying to write my own piece of code. I am currently having an issue, that is, by clicking on the Print button on my webpage, it opens a new window with a printable area. I am trying to switch to this window using SwitchTo command. But it times out every time. How do I solve this?

Mar 8 in Selenium by Vaishnavi
• 1,160 points
4,891 views

1 answer to this question.

0 votes

Hey, Vaishnavi! You could try to disable the Chrome PDF plugin and download the 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);

Or you can add the options.AddArgument("---printing"); to automatically click the print button.

This is working for me.

Hope this was helpful

answered Mar 8 by Surya
• 970 points
if u are using chrome in head mode then u can use robotjs to save that file.

Hi Akshansh, using robotjs to save or download a file in chrome head mode, can be done only if you are well-versed with node.js. As per the question's requirement, this automation needs to be done using Selenium and not using any JS framework. 

Hello Akshansh can you please explain in-detail or show some example, how robotjs can be used to download a file in chrome browser?

Related Questions In Selenium

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

How to launch Chrome browser using Selenium Webdriver on MAC?

Hey Lucas, you can launch chrome browser ...READ MORE

answered Jul 18 in Selenium by Abha
• 27,180 points
352 views
0 votes
1 answer

How to control Chrome Downloads page Using Selenium c#?

In your case you must replace 'cancel' ...READ MORE

answered May 7, 2018 in Selenium by Samarpit
• 5,130 points
1,796 views
0 votes
1 answer

Finding WebDriver element with Class Name in java

The better way to handle this element ...READ MORE

answered Apr 10, 2018 in Selenium by nsv999
• 5,110 points
965 views
0 votes
1 answer
0 votes
1 answer

Geo-location microphone camera pop up

To Allow or Block the notification, access using Selenium and you have to ...READ MORE

answered May 11, 2018 in Selenium by Samarpit
• 5,130 points
1,125 views
0 votes
1 answer

How to use such xpath to find web elements

It's much simpler to use: #MyDiv123 > div.super or ...READ MORE

answered Jun 14, 2018 in Selenium by Samarpit
• 5,130 points
868 views
0 votes
1 answer

How can I automate Google Chrome using Selenium RC?

Chrome is used to launch a special ...READ MORE

answered Mar 6 in Selenium by Surya
• 970 points
139 views
0 votes
1 answer