我正在使用 Selenium 下载通过许多复杂的登录层和其他浏览器操作访问的嵌入式 pdf。我已经根据其他各种帖子的说明使用以下选项设置了我的 chromedriver:
chromedriver = r'C:\Users\cj9250\AppData\Local\Continuum\anaconda3\chromedriver.exe'
download_dir = "C:\\Users\\CJ9250\\Downloads\\" # for linux/*nix, download_dir="/usr/Public"
options = webdriver.ChromeOptions()
profile = {
"plugins.plugins_list": [{"enabled": False, "name": "Chrome PDF Viewer"}],
"download.default_directory": download_dir ,
"download.extensions_to_open": "applications/pdf",
"plugins.always_open_pdf_externally": True,
"download.prompt_for_download": False,
"safebrowsing.enabled": True
}
options.add_experimental_option("prefs", profile)
browser = webdriver.Chrome(chromedriver, chrome_options=options)
但是,在下载到我指定的目录之前,我必须单击此框:
“打开”元素没有我可以通过检查器找到的 xpath。我猜这是 ChromeDriver 的某种内部安全设置,但我找不到绕过它的方法。
我的最终目标只是在打开的 Selenium 测试页面中下载嵌入的 PDF,这似乎是唯一建议的行动方案。
报告Sho.do OPEN
我不确定它为什么起作用,但是,我将我的配置文件变量修改为:
profile = {
"plugins.plugins_list": [{"enabled": False, "name": "Chrome PDF Viewer"}], # Disable Chrome's PDF Viewer
"download.default_directory": download_dir ,
"download.extensions_to_open": "applications/pdf",
"safebrowsing.enabled": False
}
从那里我能够使用打开按钮在页面上获得一个框架元素。元素属性之一有一个 URL。当我指示浏览器转到 URL 时,它会将文件下载到我指定的目录!
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句