0

I want to have selenium: open a page, sleep for 10 seconds (ensure everything has loaded), then print the webpage. Neither of the two solutions, using javascript to print the page, or using send_keys works.

profile = FirefoxProfile(PATH_TO_PROFILE_HERE)
profile.set_preference("print.always_print_silent", True)
profile.set_preference("print.printer_Microsoft_Print_to_PDF.print_to_file", True)
profile.set_preference("print.printer_Microsoft_Print_to_PDF.print_to_filename", file_location)
driver = webdriver.Firefox(profile)

driver.get(URL_HERE)
time.sleep(10)

# Neither one of these two seem to work.
# driver.execute_script('window.print();')
# driver.find_element_by_css_selector("body").send_keys(Keys.CONTROL, "p")

When I manually go on to the problem page (using the profile with the preferences set appropriately), I'm able to use Ctrl+P and it prints without a dialogue box on where to print/what to name it.

The problem is intermittent, it will work on www.google.com but not some other websites? Has anyone found a way to get print to work 100% of the time?

Related Problem: Automate print/save web page as pdf in chrome - python 2.7

ben.weller
  • 33
  • 6

1 Answers1

0

Instead of using selenium you can try to use this:

https://pypi.org/project/pdfkit/0.4.1/

and then this code:

import pdfkit
pdfkit.from_url('http://google.com', 'out.pdf')
Nimantha
  • 6,405
  • 6
  • 28
  • 69
Faizan Shah
  • 141
  • 16