【问题标题】:Avoid download file dialog box in Python/selenium automation避免 Python/selenium 自动化中的下载文件对话框
【发布时间】:2012-11-17 04:49:24
【问题描述】:

我正在使用此代码自动化流程,该流程将在没有提示的情况下下载 Winzip 文件。 但这似乎不起作用

profile = webdriver.firefox.firefox_profile.FirefoxProfile()
profile.set_preference('browser.helperApps.neverAsk.saveToDisk',("application/zip,
                                                   application/octet-stream"))
profile.set_preference('browser.download.folderList', 2)
profile.set_preference('browser.download.dir', '/home/jack/DOWNLOAD')
self.driver = webdriver.Firefox(firefox_profile=profile)

我仍然看到在自动化时打开的对话框。

【问题讨论】:

  • 如果您在第一次下载时手动禁用提示,之后的下载是否正常工作?
  • @root 不,我也无法手动完成。此外,browser.download.dir 中不存在 about:config
  • 如果你在 linux 上,下面的命令会给你什么curl -I https://www.downloadadress.com/ | grep Content-Type
  • 您是否也尝试过将application/x-zip-compressedapplication/x-rar-compressed 作为 MIME 类型?
  • @RocketDonkey 是的,我试过了,但很有道理。

标签: python selenium automation


【解决方案1】:

我通过提取下载链接的href并使用pythonurllib模块解决了这种情况。

使用以下代码,可以下载文件并将其保存为不同的文件名。

import urllib
url = driver.find_element_by_link_text("Download").get_attribute("href");
urllib.urlretrieve(url, "saveasfilename") 

【讨论】:

  • @cppcoder 我们也可以提供目标文件夹吗?
猜你喜欢
  • 2014-09-11
  • 2018-07-08
  • 2021-06-25
  • 2012-09-18
  • 1970-01-01
  • 1970-01-01
  • 2010-10-02
  • 1970-01-01
相关资源
最近更新 更多