我有一个python脚本,它试图自动下载excel文件。我试图直接解决firefox配置文件并且工作正常:
path = '\\path\\to\\firefox\\default\\profile\\'
fp = webdriver.FirefoxProfile(path)
driver = webdriver.Firefox(firefox_profile=fp)
driver.maximize_window()
driver.get('url')
driver.find_element_by_id('downloadButton').click()
time.sleep(5)
driver.close()
但是当我尝试以编程方式设置配置文件时,它不起作用;还有下载弹出窗口:
fp = webdriver.FirefoxProfile()
fp.set_preference('browser.download.folderList', 2)
fp.set_preference('browser.download.manager.showWhenStarting', False)
fp.set_preference('browser.download.dir', os.getcwd())
fp.set_preference('browser.helperApps.neverAsk.saveToDisk', 'text/csv/xls/xlsx')
driver = webdriver.Firefox(firefox_profile=fp)
driver.maximize_window()
driver.get('url')
driver.find_element_by_id('downloadButton').click()
time.sleep(5)
driver.close()
我还将行fp.set_preference('browser.helperApps.neverAsk.saveToDisk', 'text/csv/xls/xlsx')
更改为fp.set_preference('browser.helperApps.neverAsk.saveToDisk', ('application/vnd.ms-excel'))
;但没有变化。
知道我错了什么?
编辑1:
我试过了
fp.set_preference('browser.helperApps.neverAsk.saveToDisk', "text/plain, application/vnd.ms-excel, text/csv, text/comma-separated-values, application/octet-stream")
和
fp.set_preference('browser.helperApps.neverAsk.saveToDisk', "text/plain, application/vnd.ms-excel, text/csv, text/comma-separated-values, application/octet-stream, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
结果没有变化。
答案 0 :(得分:6)
根据about:config
文档,browser.helperApps.neverAsk.saveToDisk
首选项值应为以逗号分隔的MIME类型列表。
这个应该适合你:
fp.set_preference('browser.helperApps.neverAsk.saveToDisk', "text/plain, application/vnd.ms-excel, text/csv, text/comma-separated-values, application/octet-stream")
演示(使用XLSX Sample
网页):
import os
from selenium import webdriver
import time
url = "http://file-sample.com/xlsx/"
# configuring profile
fp = webdriver.FirefoxProfile()
fp.set_preference('browser.download.folderList', 2)
fp.set_preference('browser.download.manager.showWhenStarting', False)
fp.set_preference('browser.download.dir', os.getcwd())
fp.set_preference('browser.helperApps.neverAsk.saveToDisk', 'text/plain, application/vnd.ms-excel, text/csv, text/comma-separated-values, application/octet-stream, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')
# open the web page and download the file
driver = webdriver.Firefox(firefox_profile=fp)
driver.maximize_window()
driver.get(url)
driver.find_element_by_xpath('//div[@class="post-entry"]//a').click()
time.sleep(5)
driver.close()
结果,文件将下载到当前工作目录中。
答案 1 :(得分:1)
这项工作对我来说在Java中很好,我认为这将在Python中运行。
FirefoxProfile firefoxProfile = new FirefoxProfile();
firefoxProfile.setAcceptUntrustedCertificates(true);
firefoxProfile.setPreference("browser.download.folderList",2);
firefoxProfile.setPreference("browser.download.manager.showWhenStarting",false);
firefoxProfile.setPreference("browser.download.dir","C:\\Users\\Administrateur\\Downloads\\");
firefoxProfile.setPreference("browser.helperApps.neverAsk.saveToDisk","text/plain, application/vnd.ms-excel, text/csv, text/comma-separated-values, application/octet-stream");
driver = new FirefoxDriver(firefoxProfile);