简体   繁体   中英

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

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()

Fix these lines:

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

And

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

Problem solved <3

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM