Cannot download PDF file in to the specified directory using selenium with python in Firefox browser, pdf file opens in browser window itself

Question:

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()
Asked By: AbiSaran

||

Answers:

Fix these lines:

options.set_preference("browser.download.dir", 'C:\Temp\PdfDownload')

And

options.set_preference("pdfjs.disabled", True)

Problem solved <3

Answered By: Shayan Sakha
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.