Phoenix
Phoenix

Reputation: 4646

Python - Selenium - Print Webpage

How do I print a webpage using selenium please.

import time
from selenium import webdriver

# Initialise the webdriver
chromeOps=webdriver.ChromeOptions()
chromeOps._binary_location = "C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe"
chromeOps._arguments = ["--enable-internal-flash"]
browser = webdriver.Chrome("C:\\Program Files\\Google\\Chrome\\Application\\chromedriver.exe", port=4445, chrome_options=chromeOps)
time.sleep(3)

# Login to Webpage
browser.get('www.webpage.com')

Note: I am using the, at present, current version of Google Chrome: Version 32.0.1700.107 m

Upvotes: 5

Views: 18815

Answers (3)

robertspierre
robertspierre

Reputation: 4371

With Selenium >= 4:

import base64
from selenium.webdriver.common.print_page_options import PrintOptions

print_options = PrintOptions()
# paper sizes in centimeters
print_options.page_width = page_width
print_options.page_height = page_height
pdf = driver.print_page(print_options=print_options)
pdf_bytes = base64.b64decode(pdf)
with open(filepath, "wb") as fh:
    fh.write(pdf_bytes)

See PR for Python bindings

Upvotes: 0

Ezekiel Kruglick
Ezekiel Kruglick

Reputation: 4686

The key "trick" is that we can execute JavaScript in the selenium browser window using the "execute_script" method of the selenium webdriver, and if you execute the JavaScript command "window.print();" it will activate the browsers print function.

Now, getting it to work elegantly requires setting a few preferences to print silently, remove print progress reporting, etc. Here is a small but functional example that loads up and prints whatever website you put in the last line (where 'http://www.cnn.com/' is now):

import time
from selenium import webdriver
import os

class printing_browser(object):
    def __init__(self):
        self.profile = webdriver.FirefoxProfile()
        self.profile.set_preference("services.sync.prefs.sync.browser.download.manager.showWhenStarting", False)
        self.profile.set_preference("pdfjs.disabled", True)
        self.profile.set_preference("print.always_print_silent", True)
        self.profile.set_preference("print.show_print_progress", False)
        self.profile.set_preference("browser.download.show_plugins_in_list",False)
        self.driver = webdriver.Firefox(self.profile)
        time.sleep(5)

    def get_page_and_print(self, page):
        self.driver.get(page)
        time.sleep(5)
        self.driver.execute_script("window.print();")

if __name__ == "__main__":
    browser_that_prints = printing_browser()
    browser_that_prints.get_page_and_print('http://www.cnn.com/')

The key command you were probably missing was "self.driver.execute_script("window.print();")" but one needs some of that setup in init to make it run smooth so I thought I'd give a fuller example. I think the trick alone is in a comment above so some credit should go there too.

Upvotes: 3

Tetrinity
Tetrinity

Reputation: 1105

While it's not directly printing the webpage, it is easy to take a screenshot of the entire current page:

browser.save_screenshot("screenshot.png")

Then the image can be printed using any image printing library. I haven't personally used any such library so I can't necessarily vouch for it, but a quick search turned up win32print which looks promising.

Upvotes: 3

Related Questions