3

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

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.
pdubois
  • 7,640
  • 21
  • 70
  • 99
  • 1
    Ur update...it doesn't save the file...??? Save the wrong file... Pls check that `a.btn.btn-small.export` is unique or not for that element...else it will save click the first one only... – Vivek Singh Feb 25 '15 at 19:44

2 Answers2

5

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 to detect the mime-type:

In [1]: import magic

In [2]: mime = magic.Magic(mime=True)

In [3]: mime.from_file("result.tsv")
Out[3]: 'text/plain'
Community
  • 1
  • 1
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
  • thanks a million. I have minor glitches. Kindly look at my update. – pdubois Feb 25 '15 at 02:13
  • 1
    @pdubois thanks for accepting. Have you solved the latest issue? – alecxe Feb 26 '15 at 05:07
  • Yes. It's the timing problem. I just add `time.sleep(10)` then it saves. – pdubois Feb 26 '15 at 05:08
  • 1
    @pdubois okay, but think about using [explicit waits](http://selenium-python.readthedocs.org/en/latest/waits.html#explicit-waits) instead of a hardcoded delay with time.sleep. – alecxe Feb 26 '15 at 05:13
1

Try:

fp = webdriver.FirefoxProfile()
fp.set_preference("browser.download.dir", download_dir)
fp.set_preference("browser.preferences.instantApply", True)
fp.set_preference("browser.helperApps.neverAsk.saveToDisk",
                  "text/plain, application/octet-stream, application/binary, text/csv, application/csv, application/excel, text/comma-separated-values, text/xml, application/xml")
fp.set_preference("browser.helperApps.alwaysAsk.force", False)
fp.set_preference("browser.download.manager.showWhenStarting", False)
fp.set_preference("browser.download.folderList", 2)

driver = webdriver.Firefox(firefox_profile=fp)
Aviel Yosef
  • 533
  • 5
  • 10