Python - Selenium - Print a web page

How to print a web page 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 current version of Google Chrome: Version 32.0.1700.107 m

+3
source share
2 answers

Until it prints the web page directly, it is easy to take a screenshot from the entire current page:

browser.save_screenshot("screenshot.png")

The image can then be printed using any image print library. I personally have not used such a library, so I can’t guarantee it, but a quick search turned out to be win32print , which looks promising.

+3
source

"" , JavaScript , "execute_script" - selenium, JavaScript "window.print();" .

, , , .. , , -, ( " http://www.cnn.com/ '):

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/')

, , , , "self.driver.execute_script (" window.print(); ")", init , , , . , , .

+1

All Articles