5

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

asked Feb 19, 2014 at 16:13
1

3 Answers 3

3

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.

answered Feb 19, 2014 at 17:20
Sign up to request clarification or add additional context in comments.

Comments

3

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.

answered Dec 10, 2014 at 0:49

1 Comment

Well, this is a few years old now, can you say something about what you tried and what happened? Was there an error generated?
1

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

answered Dec 9, 2023 at 15:14

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.