3

I've tried some of the solutions posted at this site but I still cannot make this thing work. I have to grab a PDF from a secured website. I'm able to get all the way to the page that has the button to create the PDF but I cannot find code that will let me then download the PDF. Here's what I got so far and any help is much appreciated!

    from selenium import webdriver
    from selenium.webdriver.common.by import By

    driver = webdriver.Chrome()

    driver.get("https://service.medical.barco.com/server/jsp/login")

    username = driver.find_element_by_name('j_username')
    password = driver.find_element_by_name('j_password')

    username.send_keys("XXX")
    password.send_keys("XXX")

    driver.find_element_by_css_selector('[value="Log on"]').click()

    ##makes the PDF and shows it in the Google PDF viewer

    url = "https://service.medical.barco.com/server/spring/jsp/workstation/complianceCheckDetailReport/?displayId=932610524&date=1598328417477"

    driver.get(url)

    driver.find_element_by_class_name('href-button').click()

    ##This is probably unnecessary but I thought a direct link to the created PDF could give me a variable I could then download

    pdf = "https://service.medical.barco.com/server/spring/jsp/workstation/complianceCheckDetailReport/jasper/report.pdf?format=pdf&displayId=932610524&date=1598328417477"

    driver.get(pdf)

1 Answer 1

6

Once you get to the PDF, there are high chances that Chromium/Google Chrome will open the same in its PDF.js-based viewer. In order to get around this and 'download' the PDF, try passing an instance of ChromeOptions() with the following profile properties while creating a Chrome() instance as shown:

profile = {
    'download.prompt_for_download': False,
    'download.default_directory': '/path/to/download/the/pdf',
    'download.directory_upgrade': True,
    'plugins.always_open_pdf_externally': True,
}
options = webdriver.ChromeOptions()
options.add_experimental_option('prefs', profile)
driver = webdriver.Chrome(options=options)

On a side-note, you can always use the requests module.

Sign up to request clarification or add additional context in comments.

3 Comments

THANK YOU! It worked - I had tried a bunch of different solutions that looked similar to what you gave me but they never worked. Thanks again!
In that case, please mark it as a valid answer please :)
Thank you! This param 'plugins.always_open_pdf_externally': True, solve my problem!

Your Answer

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.