繁体   English   中英

使用 Selenium/Python 下载嵌入的 PDF?

[英]Download embedded PDF using Selenium/Python?

我已经尝试了在这个网站上发布的一些解决方案,但我仍然无法使这件事起作用。 我必须从安全的网站获取 PDF。 我能够一直到达具有创建 PDF 按钮的页面,但我找不到可以让我下载 PDF 的代码。 这是我到目前为止所得到的,非常感谢任何帮助!

    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)

一旦您获得 PDF,Chromium/Google Chrome 很有可能会在其基于 PDF.js 的查看器中打开相同的文件。 为了解决这个问题并“下载”PDF,请尝试在创建Chrome()实例时传递具有以下配置文件属性的ChromeOptions() Chrome()实例,如下所示:

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)

附带说明一下,您始终可以使用requests模块。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM