我知道这个问题之前已经被问过,但在尝试建议后,我不断收到操作系统下载文件窗口。我想做的是下载 pdf 文件。我已经设置了浏览器首选项,但尽管如此,它并没有抑制操作系统窗口。
这是我编写的代码片段:
firefoxProfile = webdriver.FirefoxProfile()
firefoxProfile.set_preference('browser.download.folderList', 2)
firefoxProfile.set_preference('browser.download.manager.showWhenStarting', False)
firefoxProfile.set_preference('browser.download.dir', '/media/pinku/Pinku')
firefoxProfile.set_preference('browser.helperApps.alwaysAsk.force', False)
firefoxProfile.set_preference('browser.helperApps.neverAsk.saveToDisk',
'application/octet-stream')
self.driver = webdriver.Firefox(firefoxProfile)
我正在使用 Ubuntu 12.10、Firefox、webdriver、python
最佳答案
我认为您可能弄错了 MIME 类型。试试这个
firefoxProfile.set_preference('browser.helperApps.neverAsk.saveToDisk',
'application/pdf,application/x-pdf')
关于 pdf MIME 类型的讨论可以在 here 找到。当您尝试下载 pdf 时,您应该检查 Firefox 看到的 MIME 类型。可能是服务器设置错误!
旁注:每当出现此主题(通过 selenium webdriver 下载文件)时,我强烈建议不要这样做!阅读文章 "How To Download Files With Selenium And Why You Shouldn’t"为了一个推理。基本上建议使用其他方式来测试直接下载。
更新:我没有将两种 mime 类型放入一个字符串中,之前这是错误的。我还添加了有关检查服务器实际提供的内容的建议。
关于python - 即使通过 webdriver+python 设置浏览器首选项后也无法抑制操作系统下载文件窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18934942/