OK-6ren"> OK-我的以下代码: #!/usr/bin/env python from selenium import webdriver from selenium.webdriver.support.ui impo-6ren">
gpt4 book ai didi

python - 如何在 Python 中模拟 Firefox "Save File"-> OK

转载 作者:行者123 更新时间:2023-11-30 23:14:32 25 4
gpt4 key购买 nike

我的以下代码:

#!/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()

工作正常。以此实例结束:

enter image description here

我想要实现的最后一件事是自动保存文件。尽管我已经将 Firefox 配置文件设置在顶部的代码,它没有像我希望的那样。正确的做法是什么?

更新:

alecxe 的解决方案有效。除非我尝试过这个,否则它不会保存文件。

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.

最佳答案

这个特定的对话框无法通过 selenium 进行控制 - 这是一个浏览器弹出窗口,而不是 JavaScript 弹出窗口(可以使用 swith_to.alert 自动实现)。

在这种情况下,您需要首先避免显示弹出窗口,并通过调整浏览器的所需功能(也称为配置文件首选项)让 Firefox 自动下载文件。 Firefox 可以根据正在下载的文件的mime-type自动下载文件。在您的情况下,它是 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)
<小时/>

仅供引用,我已手动下载该文件并使用 magic moduledetect 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'

关于python - 如何在 Python 中模拟 Firefox "Save File"-> OK,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28709470/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com