[英]Selenium in Python to download file: even after setting Firefox Profile the Download Window opens
I am trying to use Selenium in Python to download a file from a website.我正在尝试在 Python 中使用 Selenium 从网站下载文件。 In order to do that, I have read that I need to change the settings in my Firefox Profile to avoid opening the download dialogue window.为了做到这一点,我读到我需要更改我的 Firefox 配置文件中的设置以避免打开下载对话窗口。 I provided sample code below.我在下面提供了示例代码。 This code works absolutely great at home, but it does not function properly with my work PC.这段代码在家里工作得非常好,但在我的工作电脑上却不能正常工作。 I am suspecting that somehow Python can not change the settings of the firefox profile, even though the code below does not throw an error but rather works fine and in the end opens the download dialogue window.我怀疑 Python 以某种方式无法更改 firefox 配置文件的设置,即使下面的代码没有抛出错误而是工作正常并且最终打开了下载对话窗口。
from selenium import webdriver
import os
profile = webdriver.FirefoxProfile("C:\\Users\\Ric\\Documents\\Python Scripts\\FirefoxProfileCopies\\ric.copy")
profile.set_preference('browser.download.folderList', 2)
profile.set_preference('browser.download.manager.showWhenStarting', False)
profile.set_preference('browser.download.dir', os.getcwd())
profile.set_preference('browser.helperApps.neverAsk.saveToDisk',('application/vnd.ms-excel'))
browser = webdriver.Firefox(profile)
browser.get("http://www.sample-videos.com/download-sample-xls.php")
elem1 = browser.find_element_by_css_selector(".push-form > table:nth-child(2) > tbody:nth-child(2) > tr:nth-child(4) > td:nth-child(4) > a:nth-child(1)")
elem1.click()
This code works perfectly with my Firefox and its profile at home, but not with my computer at work.此代码与我的 Firefox 及其在家中的配置文件完美配合,但不适用于我工作的计算机。 Does anybody know why this might be?有人知道为什么会这样吗? Thank you in advance.先感谢您。
EDIT编辑
I tried to add all the MIME
types from the Microsoft webpage, but still, the download manager window opens.我尝试从 Microsoft 网页添加所有MIME
类型,但仍然打开下载管理器窗口。 When stopping the code to execute before opening the download link and trying to look at the settings for the used firefox profile with about:config
the following values are displayed:在打开下载链接之前停止执行代码并尝试使用about:config
查看使用的 Firefox 配置文件的设置时,将显示以下值:
So, after a lot of trying, I figured to look at the firefox settings in Firefox again, since it worked with an empty profile.因此,经过多次尝试,我想再次查看 Firefox 中的 Firefox 设置,因为它使用的是空配置文件。 I managed to resolve my issue and finally have the download window disappear by going to firefox, settings and changing the settings for applications:我设法解决了我的问题,最后通过转到 Firefox、设置和更改应用程序的设置使下载窗口消失:
Then, when opening this menu, search for excel and change the values from "asking every time" to "save file/download file".然后,打开此菜单时,搜索 excel 并将值从“每次询问”更改为“保存文件/下载文件”。 Sorry if these entries in the list differ from the actual ones in firefox but my Firefox is in German.抱歉,如果列表中的这些条目与 firefox 中的实际条目不同,但我的 Firefox 是德语的。 After doing this, my issue was resolved.这样做之后,我的问题就解决了。 I hope it resolves somebody else :) and thanks to anderson.我希望它可以解决其他人:) 并感谢安德森。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.