selenium pdf download returns blank pdf

Viewed 155

I use the following code to download a pdf from a web url using selenium. However, the pdf downloaded is one page and blank besides a header and footer. How can I get selenium to download the pdf correctly?

from selenium import webdriver
import json

chrome_options = webdriver.ChromeOptions()
settings = {
       "recentDestinations": [{
            "id": "Save as PDF",
            "origin": "local",
            "account": "",
        }],
        "selectedDestinationId": "Save as PDF",
        "version": 2
    }
prefs = {'printing.print_preview_sticky_settings.appState': json.dumps(settings)}
chrome_options.add_experimental_option('prefs', prefs)
chrome_options.add_argument('--kiosk-printing')
CHROMEDRIVER_PATH = '/usr/local/bin/chromedriver'
driver = webdriver.Chrome(options=chrome_options, executable_path=CHROMEDRIVER_PATH)
driver.get("https://atip.piercecountywa.gov/#/propertySummary/0420066001")
driver.execute_script('window.print();')
driver.quit()
1 Answers

I'm currently fighting with printing in headless Chrome as well, so I can't test. But the page dynamically loads a lot of stuff, so maybe it's just that you have to wait a bit before printing? You print immediately after the HTML has been retrieved, but most of the page will not be rendered at this point.

Related