Python 3.x 即使设置了首选项,python selenium也不会绕过firefox下载对话框
我正在尝试从以下链接下载文件(市场总结(关闭)-ZIP): 这是我的密码:Python 3.x 即使设置了首选项,python selenium也不会绕过firefox下载对话框,python-3.x,selenium,web-scraping,Python 3.x,Selenium,Web Scraping,我正在尝试从以下链接下载文件(市场总结(关闭)-ZIP): 这是我的密码: from datetime import date from selenium import webdriver from selenium.webdriver.common.keys import Keys from selenium.common.exceptions import ElementClickInterceptedException from selenium.webdriver.common.by
from datetime import date
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import ElementClickInterceptedException
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException
import time
profile = webdriver.FirefoxProfile()
profile.set_preference("browser.preferences.instantApply",True)
profile.set_preference("browser.helperApps.neverAsk.saveToDisk", "text/csv")
profile.set_preference("browser.helperApps.alwaysAsk.force",False)
profile.set_preference("browser.download.manager.showWhenStarting",False)
profile.set_preference("browser.download.folderList",0)
driver = webdriver.Firefox(firefox_profile=profile)
driver.get('https://dps.psx.com.pk/downloads')
start_date = driver.find_element_by_id('downloadsDatePicker')
start_date.send_keys(Keys.BACKSPACE*10)
send_date = date(2014,1,1).strftime("%Y-%m-%d")
start_date.send_keys(send_date)
driver.find_element_by_id('downloadsSearchBtn').click()
link = '/download/mkt_summary/{}.Z'.format(send_date)
time.sleep(5)
driver.find_element_by_css_selector("a[href='{}'".format(link)).click()
尽管我已经设置了首选项,firefox仍然会弹出一个下载对话框。为什么会这样?我怎样才能避免这种行为?根据您正在准备的站点和链接,您不是下载csv文件,而是下载扩展名为.Z的zip文件 您需要更改首选项
browser.helperApps.neverAsk.saveToDisk的代码,以允许从站点接收响应,如下所示
profile.set_preference("browser.helperApps.neverAsk.saveToDisk", "application/octet-stream")
我也试过:“application/x-z”、“application/x-compress”、“application/z”,但这些似乎都不起作用。