[英]Cannot download PDF file in to the specified directory using selenium with python in Firefox browser, pdf file opens in browser window itself
[英]Selenium in Python to download file: even after setting Firefox Profile the Download Window opens
我正在嘗試在 Python 中使用 Selenium 從網站下載文件。 為了做到這一點,我讀到我需要更改我的 Firefox 配置文件中的設置以避免打開下載對話窗口。 我在下面提供了示例代碼。 這段代碼在家里工作得非常好,但在我的工作電腦上卻不能正常工作。 我懷疑 Python 以某種方式無法更改 firefox 配置文件的設置,即使下面的代碼沒有拋出錯誤而是工作正常並且最終打開了下載對話窗口。
from selenium import webdriver
import os
profile = webdriver.FirefoxProfile("C:\\Users\\Ric\\Documents\\Python Scripts\\FirefoxProfileCopies\\ric.copy")
profile.set_preference('browser.download.folderList', 2)
profile.set_preference('browser.download.manager.showWhenStarting', False)
profile.set_preference('browser.download.dir', os.getcwd())
profile.set_preference('browser.helperApps.neverAsk.saveToDisk',('application/vnd.ms-excel'))
browser = webdriver.Firefox(profile)
browser.get("http://www.sample-videos.com/download-sample-xls.php")
elem1 = browser.find_element_by_css_selector(".push-form > table:nth-child(2) > tbody:nth-child(2) > tr:nth-child(4) > td:nth-child(4) > a:nth-child(1)")
elem1.click()
此代碼與我的 Firefox 及其在家中的配置文件完美配合,但不適用於我工作的計算機。 有人知道為什么會這樣嗎? 先感謝您。
編輯
我嘗試從 Microsoft 網頁添加所有MIME
類型,但仍然打開下載管理器窗口。 在打開下載鏈接之前停止執行代碼並嘗試使用about:config
查看使用的 Firefox 配置文件的設置時,將顯示以下值:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.