【问题标题】:Downloading a PDF using Selenium, Chrome and Python使用 Selenium、Chrome 和 Python 下载 PDF
【发布时间】:2019-05-28 15:35:27
【问题描述】:

我尝试关注以前有关此主题的帖子,例如这些(post 1post 2),但我仍然卡住了。

我的脚本必须使用一组凭据登录网站,然后浏览一些下拉菜单以选择报告。选择报告后,将弹出一个新窗口,必须在其中调整参数以生成报告。设置好参数后,相同的弹出窗口会以 PDF 格式生成的报告刷新,并使用 Chrome 的内置 PDF 查看器显示。我的印象是,将某些选项传递给 webdriver 会禁用此 PDF 查看器并只需下载文件,但 PDF 查看器仍在显示,并且不会自动下载任何内容。当然,我错过了一些东西,或者我写错了一些东西。这是我的代码的 jist:

from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.chrome.options import Options

chrome_options = Options()
chrome_options.add_experimental_option('prefs',  {
    "download.default_directory": download_dir,
    "download.prompt_for_download": False,
    "download.directory_upgrade": True,
    "plugins.plugins_disabled": ["Chrome PDF Viewer"]
    }
)

browser = webdriver.Chrome(options = chrome_options)

driver = webdriver.Chrome()
driver.get(url)

#In between here are a bunch of steps here that navigates through drop down menus

#This step may not be necessary, but I figured I'd include it to address when the pop up window refreshes and displays the report in PDF format through Chrome's PDF viewer
driver.switch_to.window(driver.window_handles[1])

因此,即使我之前禁用了它,Chrome 仍会显示 PDF 查看器。没有下载任何内容,所以我想知道是否需要提供另一行代码或其他内容。

在 Windows 10 上使用 Selenium 版本 3.141.0、Python 3.6.4、Chrome webdriver 2.45。

【问题讨论】:

  • 我遇到了类似的问题,但是,在 .Net 中 - 所以我没有 Python 答案给你(因此这个评论),但通常你需要将以下命令传递给 Chrome通过硒:Page.printToPDF。我的 .Net 调用看起来像:dynamic result = new DDict(chrome.ExecuteChromeCommandWithResult("Page.printToPDF", printToPdfOpts)); 然后 File.WriteAllBytes(pdfPath, Convert.FromBase64String(result.data)); chromedevtools.github.io/devtools-protocol/tot/Page/…

标签: python selenium selenium-chromedriver


【解决方案1】:

你需要替换"plugins.plugins_disabled": ["Chrome PDF Viewer"]

与:

"plugins.always_open_pdf_externally": True

希望对您有所帮助!

【讨论】:

  • 我尝试了以下更改,但它仍然在 Chrome PDF 查看器中打开 PDF:chrome_options.add_experimental_option('prefs', { "download.default_directory": download_dir, "download.prompt_for_download": False, "plugins.always_open_pdf_externally": True } )
  • @sangharsh 你用的是什么版本?
  • 成功了。我的代码中还有其他问题。找到并更正。现在这个解决方案有效。迄今为止最好的一个!谢谢!!我已经投票了
  • 我面临同样的问题。我已经使用了上面的代码行,它仍然在新窗口中打开 PDF 而不是下载它。该报告是通过 Microstrategy 生成的。
  • 嗨@Omega,您是否将选项传递给驱动程序实例?
【解决方案2】:

我有一个类似的问题,我用 Java 中的 firefox 驱动程序解决了这个问题。 这是我的代码:

ffprofile.setPreference("browser.helperApps.neverAsk.saveToDisk","application/pdf");
ffprofile.setPreference("browser.download.folderList", 2);
ffprofile.setPreference("browser.download.manager.showWhenStarting", false);
ffprofile.setPreference("browser.download.dir", "path/to/directory");
ffprofile.setPreference("plugin.scan.plid.all",false);
ffprofile.setPreference("plugin.scan.Acrobat","99.0");
ffprofile.setPreference("pdfjs.disabled",true);

也许你可以选择使用 Firefox 并且 Java->Python 翻译应该很简单。

【讨论】:

  • 这个java>python翻译怎么做?
猜你喜欢
  • 2019-04-12
  • 1970-01-01
  • 2022-09-27
  • 1970-01-01
  • 2020-08-04
  • 2021-11-09
  • 1970-01-01
  • 2018-01-19
  • 2022-06-22
相关资源
最近更新 更多