python - 在另一个浏览器页面上打开时如何在python中使用selenium自动下载PDF文件
问题描述
我正在单击带有 selenium 的链接,它正在打开一个带有 PDF 的新浏览器选项卡,我想知道是否有办法下载该 PDF,我不在乎浏览器选项卡是否打开然后开始下载,我想要的是下载那个PDF。
谢谢
解决方案
你可以使用 pyautogui 和 Options
import pyautogui
from selenium.webdriver.chrome.options import Options
DRIVER_PATH = r'chromedriver.exe' //chromedriver path
chrome_options = Options()
chrome_options.add_experimental_option('prefs', {
"download.default_directory": "C:/Users", #Change default directory for downloads
"download.prompt_for_download": False, #To auto download the file
"download.directory_upgrade": True,
"plugins.always_open_pdf_externally": True #It will not show PDF directly in chrome
})
driver = webdriver.Chrome(executable_path=DRIVER_PATH,options = chrome_options)
driver.get(url) //url of pdf
time.sleep(3)
pyautogui.press('enter')
推荐阅读
- r - R lm() 模型中的幻象缺失
- r - 如何使用 ggplot2 制作精美的箱线图?
- javascript - 仅从 json mongo 替换年份 isoDate
- python - 当origin ='julian'时在熊猫to_datetime origin参数上获取OutofBoundSDateTime
- ios - Swift 泛型。如何指定输出类型?
- android - Kotlin:类型推断失败。预期类型不匹配:推断类型为 MutableList
但是 MutableCollection 预计 - c++ - BGRA 缓冲液到 AYUV 的最佳转换
- validation - 日期选择器验证开始日期
- html - 我遇到了网格问题并移动了我的 div
- sql - 其中 col1 from tab1 和 col1 from tab2 in select from tab3