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, 2019 in Selenium by Vaishnavi
• 1,180 points
24,160 views

2 answers 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. For further understanding, you can refer to the Selenium Course.

Hope this was helpful

answered Mar 8, 2019 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?

tell me more about this options.AddArgument("---printing");

Hi, @Rj,

Could you please share your query in detail as it's very tough to find the relevance of your query asked,. Kindly share your workaround.
0 votes
Vaishanvi,

Select  ID  selected dropdown or it may pass with DropDown.selectByVisibleText("Save as PDF");orDropDown.selectByIndex(1)
answered Feb 21, 2020 by Abdul

Related Questions In Selenium

0 votes
1 answer

How to extract text from a web page using selenium and save it as a text file?

Hello Isha, you can checkout this code ...READ MORE

answered May 7, 2019 in Selenium by Anvi
• 14,150 points
28,355 views
0 votes
1 answer

How to save the content on the page (full page) using Selenium WebDriver?

Selenium isn't designed to do this, you ...READ MORE

answered Jun 28, 2019 in Selenium by Vaishnavi
• 1,180 points
1,432 views
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, 2019 in Selenium by Abha
• 28,040 points
3,547 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,890 points
6,262 views
0 votes
2 answers

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,520 points
5,613 views
0 votes
2 answers

Problem while using InternetExplorerDriver in Selenium WebDriver

enable trusted connection  in internet explorer by ...READ MORE

answered Aug 31, 2020 in Selenium by Sri
• 3,100 points
6,197 views
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,890 points
3,845 views
0 votes
2 answers

How to use such xpath to find web elements

xpath are two types. 1) Absolute XPath:    /html/b ...READ MORE

answered Sep 2, 2020 in Selenium by Sri
• 3,100 points
4,747 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, 2019 in Selenium by Surya
• 970 points
625 views
0 votes
1 answer

How to check if an image is displayed on web page while working with Selenium WebDriver?

I've come across a similar situation before, where the image ...READ MORE

answered May 10, 2019 in Selenium by Surya
• 970 points
5,605 views