【发布时间】:2019-05-28 15:35:27
【问题描述】:
我尝试关注以前有关此主题的帖子,例如这些(post 1,post 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