Downloading a PDF using Selenium, Chrome and Python(使用Selenium、Chrome和Python下载PDF)
本文介绍了使用Selenium、Chrome和Python下载PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试着关注以前关于这个主题的帖子,比如这些(post 1,post 2),但是我还是卡住了。 我的脚本必须使用一组凭据登录到站点,然后在一些下拉菜单中导航以选择报告。选择报告后,将弹出一个新窗口,必须在其中调整参数以生成报告。一旦设置了参数,相同的弹出窗口将刷新为PDF格式的生成报告,并使用Chrome的内置PDF查看器显示。我的印象是,将某些选项传递给Webdriver会禁用此PDF查看器并简单地下载文件,但PDF查看器仍在显示,并且不会自动下载任何内容。我肯定是遗漏了什么,或者我写错了什么。下面是我的代码的摘要:
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。
推荐答案
您需要更换"plugins.plugins_disabled": ["Chrome PDF Viewer"]
使用:
"plugins.always_open_pdf_externally": True
希望这对您有帮助!
这篇关于使用Selenium、Chrome和Python下载PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
沃梦达教程
本文标题为:使用Selenium、Chrome和Python下载PDF


基础教程推荐
猜你喜欢
- 使 Python 脚本在 Windows 上运行而不指定“.py";延期 2022-01-01
- Python 的 List 是如何实现的? 2022-01-01
- 如何在Python中绘制多元函数? 2022-01-01
- 症状类型错误:无法确定关系的真值 2022-01-01
- 合并具有多索引的两个数据帧 2022-01-01
- 哪些 Python 包提供独立的事件系统? 2022-01-01
- 使用Python匹配Stata加权xtil命令的确定方法? 2022-01-01
- 将 YAML 文件转换为 python dict 2022-01-01
- 使用 Google App Engine (Python) 将文件上传到 Google Cloud Storage 2022-01-01
- 如何在 Python 中检测文件是否为二进制(非文本)文 2022-01-01