How to emulate Firefox "Save File" -> OK in Python
Question: My following code:

    
    
    #!/usr/bin/env python
    from selenium import webdriver
    from selenium.webdriver.support.ui import Select
    from selenium.webdriver.common.by import By
    
    # Define firefox profile
    download_dir = "/Users/pdubois/Desktop/TargetMine_Output/"
    fp = webdriver.FirefoxProfile()
    fp.set_preference("browser.download.folderList",2)
    fp.set_preference("browser.download.manager.showWhenStarting",False)
    fp.set_preference("browser.download.dir", download_dir)
    #fp.set_preference("browser.helperApps.neverAsk.saveToDisk", "text")
    fp.set_preference("browser.helperApps.neverAsk.saveToDisk", "text/plain")
    
    
    driver = webdriver.Firefox(fp)
    driver.implicitly_wait(20)
    
    genes  = "Eif2ak2,Pcgf5,Vps25,Prmt6,Tcerg1,Dnttip2,Preb,Polr1b,Gabpb1,Prdm1,Fosl2,Zfp143,Psip1,Kat6a,Tgif1,Txn1,Irf8,Cnot6l,Zfp451,Foxk2,Lpxn,Etv6,Khsrp,Lmo4,Nkrf,Mafk,Mbd1,Cited2,Elp5,Jdp2,Bzw1,Rbm15b,Klf9,Gtf2e2,Dynll1,Klf6,Stat1,Srrt,Gtf2f1,Adnp2,Ikbkg,Mybbp1a,Nup62,Brd2,Chd1,Kctd1,Sap30,Cebpd,Mtf1,Gtf2h2,Fubp1,Tcea1,Irf2bp2,Ezh2,Hnrpdl,Pml,Cebpz,Med7"
    targetmine_url = "http://targetmine.nibio.go.jp/targetmine/begin.do"
    driver.get(targetmine_url)
    
    # Define type of list to be submitted
    gene_select = Select(driver.find_element_by_name("type"))
    gene_select.select_by_visible_text(u"Gene")
    
    
    # Enter list and submit
    gene_input = driver.find_element_by_id("listInput")
    gene_input.send_keys(genes)
    submit = driver.find_element_by_css_selector("input.button.light").click()
    
    # Choose name for list
    driver.find_element_by_id("newBagName").clear()
    driver.find_element_by_id("newBagName").send_keys("ADX.06.ID.Clust1")
    
    driver.switch_to_frame("__pomme::0")
    # Add All
    driver.find_element_by_css_selector("span.small.success.add-all.button").click()
    # Save all genes
    driver.find_element_by_css_selector("a.success.button.save").click()
    # Select M. Musculus
    driver.find_element_by_xpath("//ul[@id='customConverter']/li[2]/a[1]").click()
    
    # Gene enrchment part
    go_xpath  = "//div[@id='gene_go_enrichment-widget']/div[@class='inner']/div[1]/div[@class='form']/form[@style='margin:0']/div[2]/select[1]"
    #driver.find_element_by_xpath(go_xpath).click()
    go_select = Select(driver.find_element_by_xpath(go_xpath))
    go_select.select_by_visible_text(u"1.00")
    
    
    # Download 
    #driver.find_element_by_css_selector("a.btn.btn-small.export").click()
    

Works fine. Which end with this instances:

![enter image description here](http://i.stack.imgur.com/VBvzb.jpg)

One last thing I want to achieve then is to _Save the file automatically_.
Despite I've already set the Firefox profile at the top of the code, it
doesn't do as I hoped. What's the right way to do it?

**Update:**

The solution by alecxe works. Except I tried this, it doesn't save the file.

    
    
    go_download_xpath  = "//div[@id='gene_go_enrichment-widget']/div[@class='inner']/div[1]/div[2]/a[@class='btn btn-small export']"
    
    driver.find_element_by_xpath(go_download_xpath).click()
    # it saved the specific desired file.
    # using 
    #driver.find_element_by_css_selector("a.btn.btn-small.export").click()
    #save the wrong file.
    


Answer: This particular dialog _cannot be controlled via selenium_ \- this is a
browser popup, not a javascript popup (which can be automated with
`swith_to.alert`).

In this case, you need to avoid the popup being shown in the first place and
make Firefox download the file automatically by tweaking browser's _desired
capabilities_ (aka profile preferences). Firefox can download files
automatically depending on the _mime-type_ of the file being downloaded. In
your case it is `text/plain`:

    
    
    fp = webdriver.FirefoxProfile()
    fp.set_preference("browser.download.folderList",2)
    fp.set_preference("browser.download.dir", download_dir)
    fp.set_preference("browser.download.manager.showWhenStarting", False)
    
    fp.set_preference("browser.helperApps.neverAsk.saveToDisk", "text/plain")
    
    driver = webdriver.Firefox(firefox_profile=fp)
    

* * *

FYI, I've downloaded the file manually and used [`magic`
module](https://github.com/ahupp/python-magic) to [detect the mime-
type](http://stackoverflow.com/questions/43580/how-to-find-the-mime-type-of-a-
file-in-python):

    
    
    In [1]: import magic
    
    In [2]: mime = magic.Magic(mime=True)
    
    In [3]: mime.from_file("result.tsv")
    Out[3]: 'text/plain'
    

