0

I am trying to save a csv file without the popup box. I followed the following link to figure out my mime:

http://watirmelon.com/2011/09/07/determining-file-mime-types-to-autosave-using-firefox-watir-webdriver/

I even added all the NC_values from the mimeTypes.rdf file. But, it does not seem to be working.

profile = webdriver.FirefoxProfile()
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', 'irc,ircs,webcal,application/csv,application/download,application/pdf')

mimeTypes.rdf

<RDF:RDF xmlns:NC="http://home.netscape.com/NC-rdf#"
         xmlns:RDF="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
  <RDF:Seq RDF:about="urn:schemes:root">
    <RDF:li RDF:resource="urn:scheme:webcal"/>
    <RDF:li RDF:resource="urn:scheme:ircs"/>
    <RDF:li RDF:resource="urn:scheme:mailto"/>
    <RDF:li RDF:resource="urn:scheme:irc"/>
  </RDF:Seq>
  <RDF:Description RDF:about="urn:scheme:mailto"
                   NC:value="mailto">
    <NC:handlerProp RDF:resource="urn:scheme:handler:mailto"/>
  </RDF:Description>
  <RDF:Description RDF:about="urn:handler:web:https://30boxes.com/external/widget?refer=ff&amp;url=%s"
                   NC:prettyName="30 Boxes"
                   NC:uriTemplate="https://30boxes.com/external/widget?refer=ff&amp;url=%s" />
  <RDF:Description RDF:about="urn:scheme:irc"
                   NC:value="irc">
    <NC:handlerProp RDF:resource="urn:scheme:handler:irc"/>
  </RDF:Description>
  <RDF:Description RDF:about="urn:scheme:handler:mailto"
                   NC:useSystemDefault="true"
                   NC:alwaysAsk="false">
    <NC:possibleApplication RDF:resource="urn:handler:web:https://compose.mail.yahoo.com/?To=%s"/>
    <NC:possibleApplication RDF:resource="urn:handler:web:https://mail.google.com/mail/?extsrc=mailto&amp;url=%s"/>
  </RDF:Description>
  <RDF:Description RDF:about="urn:handler:web:https://mail.google.com/mail/?extsrc=mailto&amp;url=%s"
                   NC:prettyName="Gmail"
                   NC:uriTemplate="https://mail.google.com/mail/?extsrc=mailto&amp;url=%s" />
  <RDF:Description RDF:about="urn:scheme:handler:webcal"
                   NC:useSystemDefault="true"
                   NC:alwaysAsk="true">
    <NC:possibleApplication RDF:resource="urn:handler:web:https://30boxes.com/external/widget?refer=ff&amp;url=%s"/>
  </RDF:Description>
  <RDF:Description RDF:about="urn:mimetype:application/pdf"
                   NC:value="application/pdf">
    <NC:handlerProp RDF:resource="urn:mimetype:handler:application/pdf"/>
  </RDF:Description>
  <RDF:Description RDF:about="urn:scheme:ircs"
                   NC:value="ircs">
    <NC:handlerProp RDF:resource="urn:scheme:handler:ircs"/>
  </RDF:Description>
  <RDF:Description RDF:about="urn:scheme:handler:ircs"
                   NC:alwaysAsk="true">
    <NC:possibleApplication RDF:resource="urn:handler:web:https://www.mibbit.com/?url=%s"/>
  </RDF:Description>
  <RDF:Description RDF:about="urn:mimetype:handler:application/download"
                   NC:alwaysAsk="true" />
  <RDF:Description RDF:about="urn:mimetype:application/csv"
                   NC:value="application/csv"
                   NC:editable="true"
                   NC:fileExtensions="csv"
                   NC:description="Microsoft Excel Comma Separated Values File">
    <NC:handlerProp RDF:resource="urn:mimetype:handler:application/csv"/>
  </RDF:Description>
  <RDF:Description RDF:about="urn:schemes">
    <NC:Protocol-Schemes RDF:resource="urn:schemes:root"/>
  </RDF:Description>
  <RDF:Description RDF:about="urn:handler:web:https://compose.mail.yahoo.com/?To=%s"
                   NC:prettyName="Yahoo! Mail"
                   NC:uriTemplate="https://compose.mail.yahoo.com/?To=%s" />
  <RDF:Description RDF:about="urn:handler:web:https://www.mibbit.com/?url=%s"
                   NC:prettyName="Mibbit"
                   NC:uriTemplate="https://www.mibbit.com/?url=%s" />
  <RDF:Seq RDF:about="urn:mimetypes:root">
    <RDF:li RDF:resource="urn:mimetype:application/pdf"/>
    <RDF:li RDF:resource="urn:mimetype:application/download"/>
    <RDF:li RDF:resource="urn:mimetype:application/csv"/>
  </RDF:Seq>
  <RDF:Description RDF:about="urn:scheme:handler:irc"
                   NC:alwaysAsk="true">
    <NC:possibleApplication RDF:resource="urn:handler:web:https://www.mibbit.com/?url=%s"/>
  </RDF:Description>
  <RDF:Description RDF:about="urn:mimetype:handler:application/csv"
                   NC:alwaysAsk="true" />
  <RDF:Description RDF:about="urn:mimetypes">
    <NC:MIME-types RDF:resource="urn:mimetypes:root"/>
  </RDF:Description>
  <RDF:Description RDF:about="urn:mimetype:application/download"
                   NC:value="application/download"
                   NC:editable="true"
                   NC:fileExtensions="csv"
                   NC:description="Microsoft Excel Comma Separated Values File">
    <NC:handlerProp RDF:resource="urn:mimetype:handler:application/download"/>
  </RDF:Description>
  <RDF:Description RDF:about="urn:scheme:webcal"
                   NC:value="webcal">
    <NC:handlerProp RDF:resource="urn:scheme:handler:webcal"/>
  </RDF:Description>
  <RDF:Description RDF:about="urn:mimetype:handler:application/pdf"
                   NC:handleInternal="true"
                   NC:alwaysAsk="false" />
  <RDF:Description RDF:about="urn:root"
                   NC:en-US_defaultHandlersVersion="4" />
</RDF:RDF>

my full code:

profile = webdriver.FirefoxProfile()
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/csv,application/download')

driver = webdriver.Firefox(profile)
driver.get(url)

time.sleep(5)

driver.switch_to.frame("main")
elem = driver.find_element_by_name('username')
elem.send_keys(username)
elem = driver.find_element_by_name("password")
elem.send_keys(password)
elem.send_keys(Keys.RETURN)

driver.find_element_by_xpath(".//*[@id='wrap']/div/div/div/div/div/div/div/div/div/div/div[2]/div/div/div/div[1]/div/span/span[2]/div[2]/table/tbody/tr/td[2]").click()
time.sleep(10)
driver.find_element_by_xpath(".//*[@id='scrollable']/div/div/div[1]/ul/li[1]/ul/li[2]/a[2]").click()
time.sleep(10)

#download report
try:
    driver.switch_to.window(driver.window_handles[1])
    element = WebDriverWait(driver,30).until(EC.presence_of_element_located((By.XPATH,".//*[@id='datablock']/div[5]/div[1]/div[10]/div[12]/input")))
    driver.find_element_by_xpath(".//*[@id='datablock']/div[5]/div[1]/div[10]/div[12]/input").clear()
    driver.find_element_by_xpath(".//*[@id='datablock']/div[5]/div[1]/div[10]/div[12]/input").send_keys(fromDate)

    driver.find_element_by_xpath(".//*[@id='datablock']/div[5]/div[1]/div[10]/div[13]/input").clear()
    driver.find_element_by_xpath(".//*[@id='datablock']/div[5]/div[1]/div[10]/div[13]/input").send_keys(toDate)

    driver.find_element_by_xpath(".//*[@id='datablock']/div[1]/div[2]/a[2]").click()
    driver.find_element_by_xpath("html/body/div[8]/ul/li/span").click()
    driver.find_element_by_xpath(".//*[@id='datablock']/div[1]/div[2]/a[3]").click()
except:
        print 'error'
collarblind
  • 3,779
  • 11
  • 25
  • 43
  • `application/csv` and `application/download` is usually all you need. Try: `profile.set_preference('browser.helperApps.neverAsk.saveToDisk', 'application/csv,application/download')`. Are you sure you are passing `profile` to the `webdriver.Firefox` instance? – alecxe Sep 16 '15 at 20:21
  • I tried that and it still does not work and yea it's passing the profile in the webdriver. driver = webdriver.Firefox(profile) – collarblind Sep 17 '15 at 12:19
  • Is this a public site and you can share the link to it (assuming it's possible to register)? – alecxe Sep 21 '15 at 19:10
  • It's a private site only available in network – collarblind Sep 21 '15 at 19:11
  • Can you upload the csv file (empty one) somewhere for us to test? – alecxe Sep 21 '15 at 19:11
  • here you go http://www.filedropper.com/report5 – collarblind Sep 21 '15 at 19:16

1 Answers1

6

magic Python module says it's text/plain:

>>> import magic
>>> mime = magic.Magic(mime=True)
>>> mime.from_file("report.csv")
'text/plain'

The final preference (also leaving application/csv and application/download and adding application/octet-stream and text/csv to be safe):

profile.set_preference('browser.helperApps.neverAsk.saveToDisk', 'text/plain,text/csv,application/csv,application/download,application/octet-stream')
Community
  • 1
  • 1
alecxe
  • 414,977
  • 106
  • 935
  • 1,083