Use Firefox 'print' or 'save as' features using Selenium WebDriver

firefox, selenium, selenium-webdriver

Solution

It is possible to enable silent printing in firefox to print to the default printer, bypassing the print dialog.

The required firefox preference is `print.always_print_silent`, and can be setup with selenium like so:

import org.openqa.selenium.JavascriptExecutor;
/* ... */
FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("print.always_print_silent", true);
WebDriver driver = new FirefoxDriver(profile);

Now simply navigate to a web page and call print using javascript:

driver.get("http://www.google.com");
((JavascriptExecutor)driver).executeScript("window.print();");

Additionally, couple this with a free PDF printer such as novaPDF to print without displaying the Save as dialog and automatically save a PDF to a predefined location.

Problem

I would like to programatically instruct Firefox to visit a list of URLs (defined in a text file, for instance) and for each of them save the page to disk or print it. I know Selenium provides a feature to capture a screenshot of the page, but I would like to know if it's possible to use browser's native saving and printing features. If Selenium does not provide such features, would any other tool allow me to define a script to be executed by Firefox and achieve similar results?

Original source