1

I have the following code:

element_search_field = browser.find_element_by_id(search_field_id)
op.set_preference("browser.download.folderList",2)
op.set_preference("browser.download.manager.showWhenStarting", False)
op.set_preference("browser.download.dir","C:\\Users\\user\\Selenium")
op.set_preference("browser.helperApps.neverAsk.saveToDisk", "application/octet-stream,application/vnd.csv")

downloadcsv =  browser.find_element_by_css_selector('#downloadOCTable')
downloadcsv.click();

I am having trouble at the last line downloadcsv.click();. I was hoping that changing "application/octet-stream,application/vnd.csv") from "application/octet-stream,application/vnd.ms-excel") would automatically save the .csv file to the downloads folder but i am still get the DialogBox. Is there anyway I can have it saved without the DiaglogBox?

Edit:

As suggested by @Prophet:

I had made these changes but i am still get the pop up

profile = FirefoxProfile() profile.set_preference("general.useragent.override", userAgent)

profile.set_preference("browser.download.dir", "C:\\Users\\[user]\\Selenium Options")
profile.set_preference("browser.helperApps.neverAsk.saveToDisk", "application/csv,application/excel,application/vnd.ms-excel,application/vnd.msexcel,text/anytext,text/comma-separated-values,text/csv,text/plain,text/x-csv,application/x-csv,text/x-comma-separated-values,text/tab-separated-values,data:text/csv")
profile.set_preference("browser.helperApps.neverAsk.saveToDisk", "application/xml,text/plain,text/xml,image/jpeg,application/octet-stream,data:text/csv")
profile.set_preference("browser.download.folderList",2)
profile.set_preference("browser.download.manager.showWhenStarting",False)
profile.set_preference("browser.helperApps.neverAsk.openFile","application/csv,application/excel,application/vnd.ms-excel,application/vnd.msexcel,text/anytext,text/comma-separated-values,text/csv,text/plain,text/x-csv,application/x-csv,text/x-comma-separated-values,text/tab-separated-values,data:text/csv")
profile.set_preference("browser.helperApps.neverAsk.openFile","application/xml,text/plain,text/xml,image/jpeg,application/octet-stream,data:text/csv")
profile.set_preference("browser.helperApps.alwaysAsk.force", False)
profile.set_preference("browser.download.useDownloadDir", True)
profile.set_preference("dom.file.createInChild", True)
Slartibartfast
  • 1,058
  • 4
  • 26
  • 60

2 Answers2

1

There are several kinds of csv applications. We can't know what will work for the specific site you are working with.
I have all these preferences set and so far it works for me in all the cases

op.set_preference("browser.download.dir", downloadsPath)
op.set_preference("browser.helperApps.neverAsk.saveToDisk", "application/csv,application/excel,application/vnd.ms-excel,application/vnd.msexcel,text/anytext,text/comma-separated-values,text/csv,text/plain,text/x-csv,application/x-csv,text/x-comma-separated-values,text/tab-separated-values,data:text/csv")
op.set_preference("browser.helperApps.neverAsk.saveToDisk", "application/xml,text/plain,text/xml,image/jpeg,application/octet-stream,data:text/csv")
op.set_preference("browser.download.folderList",2)
op.set_preference("browser.download.manager.showWhenStarting",False)
op.set_preference("browser.helperApps.neverAsk.openFile","application/csv,application/excel,application/vnd.ms-excel,application/vnd.msexcel,text/anytext,text/comma-separated-values,text/csv,text/plain,text/x-csv,application/x-csv,text/x-comma-separated-values,text/tab-separated-values,data:text/csv")
op.set_preference("browser.helperApps.neverAsk.openFile","application/xml,text/plain,text/xml,image/jpeg,application/octet-stream,data:text/csv")
op.set_preference("browser.helperApps.alwaysAsk.force", False)
op.set_preference("browser.download.useDownloadDir", True)
op.set_preference("dom.file.createInChild", True)

downloadsPath here is a path to Downloads folder

Prophet
  • 32,350
  • 22
  • 54
  • 79
0

You should use the MIME type for .csv as text/csv, you can check rfc4180

The below following type could help you

application/csv
application/x-csv
text/csv
text/comma-separated-values
text/x-comma-separated-values
text/tab-separated-values
text/plain
text/x-csv

So you can check it as below

op.set_preference("browser.helperApps.neverAsk.saveToDisk","text/csv,application/octet-stream")    

here is a great discussion happens you can check it

YaDav MaNish
  • 1,260
  • 2
  • 12
  • 20