Ulyses
Ulyses

Reputation: 121

How to automatically download a PDF file with selenium in python when it is open on another browser page

I am clicking an link with selenium and it is opening a new browser tab with a PDF, I want to know if there is a way to dowload that PDF , I don't care if the browser tab is open and then the downloading is started, what I want is to download that PDF.

Thanks

Upvotes: 0

Views: 2822

Answers (1)

shubham
shubham

Reputation: 513

you can use pyautogui and 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')

Upvotes: 2

Related Questions