[英]Download embedded PDF using Selenium/Python?
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.
我必须从安全的网站获取 PDF。 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.
我能够一直到达具有创建 PDF 按钮的页面,但我找不到可以让我下载 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)
Once you get to the PDF, there are high chances that Chromium/Google Chrome will open the same in its PDF.js-based viewer.一旦您获得 PDF,Chromium/Google Chrome 很有可能会在其基于 PDF.js 的查看器中打开相同的文件。 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:为了解决这个问题并“下载”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)
On a side-note, you can always use the requests
module.附带说明一下,您始终可以使用
requests
模块。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.