为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

慕码人2483693 2023-06-27 16:30:39
我已经尝试了该网站上发布的一些解决方案,但我仍然无法使这个东西发挥作用。我必须从安全网站获取 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)
查看完整描述

1 回答

?
ibeautiful

TA贡献1993条经验 获得超5个赞

一旦您打开 PDF,Chromium/Google Chrome 很有可能会在基于 PDF.js 的查看器中打开该 PDF。为了解决这个问题并“下载”PDF,请尝试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模块。


查看完整回答
反对 回复 2023-06-27
  • 1 回答
  • 0 关注
  • 131 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信