使用Selenium,Chrome和Python下载PDF

我的脚本必须使用一组凭据登录到站点,然后在某些下拉菜单中导航以选择报告。选择报告后,将弹出一个新窗口,必须在其中调整参数以生成报告。设置完参数后,相同的弹出窗口将以生成的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浏览器,Chrome仍会显示它。没有下载任何内容,因此我想知道是否需要提供另一行代码或其他内容。

在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 的全部内容, 来源链接: utcz.com/qa/423682.html

回到顶部