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
33,144 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 22, 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
33,103 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
4,321 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,140 points
4,992 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,910 points
9,274 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,500 points
12,616 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,190 points
8,571 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,910 points
6,629 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 3, 2020 in Selenium by Sri
• 3,190 points
7,517 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
1,318 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
7,525 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP