这是我的代码:
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
profile = webdriver.FirefoxProfile()
profile.set_preference("browser.download.folderList", 2);
profile.set_preference("browser.download.manager.showWhenStarting", False)
profile.set_preference("browser.download.dir", "/home/ripundeep/Desktop/Python Challenges /")
profile.set_preference("browser.helperApps.alwaysAsk.force", False)
profile.set_preference("browser.download.manager.alertOnEXEOpen", False)
profile.set_preference("browser.download.manager.focusWhenStarting", False)
profile.set_preference("browser.download.manager.useWindow", False)
profile.set_preference("browser.download.manager.showAlertOnComplete", False)
profile.set_preference("browser.download.manager.closeWhenDone", False)
profile.set_preference("browser.helperApps.neverAsk.openFile","text/csv")
profile.set_preference("browser.helperApps.neverAsk.saveToDisk","text/csv")
profile.update_preferences()
driver = webdriver.Firefox(firefox_profile=profile)
driver.get(url)
driver.find_element_by_css_selector("#id").send_keys("keyword")
WebDriverWait(driver, 1, poll_frequency=0.1).until(lambda drv: len(drv.find_elements_by_css_selector("#ctl00_ContentPlaceHolder1_btnSubmit")) > 0)
driver.find_element_by_css_selector("#submitid").click()
driver.find_element_by_css_selector("#DownloadLinkId").click()
我想停止firefox向我显示下载提示并自动保存,我尝试了所有可能的解决方案但是没有用。请帮忙。
答案 0 :(得分:2)
我记得提供更多mime类变体通常有助于解决这样的问题:
mime_types = [
'text/plain',
'application/vnd.ms-excel',
'text/csv',
'application/csv',
'text/comma-separated-values',
'application/download',
'application/octet-stream',
'binary/octet-stream',
'application/binary',
'application/x-unknown'
]
profile.set_preference("browser.helperApps.neverAsk.saveToDisk", ",".join(mime_types))
我还认为你不应该打电话给profile.update_preferences()
。
除此之外,以下是查看mime-type Firefox检测到的内容的步骤:
mimeTypes.rdf
文件urn:mimetype
browser.helperApps.neverAsk.saveToDisk
逗号分隔值