While trying to download a PDF file into a specific directory using Selenium with Python in Firefox browser, the pdf file is not getting downloaded into that directory, it opens in the firefox browser itself. Needs help.
Cannot use Firefox_Profile, it is deprecated.
from selenium import webdriver
from selenium.webdriver.firefox.service import Service as FirefoxService
from webdriver_manager.firefox import GeckoDriverManager
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.common.by import By
options = Options()
options.set_preference("browser.download.folderList", 2)
options.set_preference("browser.download.manager.showWhenStarting", False)
options.set_preference("browser.download.dir", 'C:/Temp/PdfDownload')
options.set_preference("browser.download.useDownloadDir", True)
options.set_preference("browser.helperApps.neverAsk.saveToDisk", 'application/pdf')
options.set_preference("pdfjs.disable", True)
driver = webdriver.Firefox(service=FirefoxService(GeckoDriverManager().install()), options = options)
driver.maximize_window()
driver.get("https://file-examples.com/index.php/sample-documents-download/sample-pdf-download/")
driver.find_element(By.XPATH,".//*[text()='Download sample pdf file']").click()
CodePudding user response:
Fix these lines:
options.set_preference("browser.download.dir", 'C:\\Temp\\PdfDownload')
And
options.set_preference("pdfjs.disabled", True)
Problem solved <3