简体   繁体   English

Python 中的 Selenium 下载文件:即使在设置 Firefox Profile 后,下载窗口也会打开

[英]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 配置文件的设置时,将显示以下值: 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.

相关问题 Cannot download PDF file in to the specified directory using selenium with python in Firefox browser, pdf file opens in browser window itself - Cannot download PDF file in to the specified directory using selenium with python in Firefox browser, pdf file opens in browser window itself 即使通过webdriver + python设置浏览器首选项后,也无法抑制OS下载文件窗口 - Cannot suppress the OS download file window even after setting browser preference through webdriver+python python selenium。 下载后如何切换回上一个窗口/标签打开一个新标签 - python selenium . how to switch back to previous window/tab after a download opens up a new tab 下载带有 Selenium 和 Firefox 的文件 - Download file with Selenium and Firefox 创建Webdriver后Selenium Firefox配置文件更新下载目录 - Selenium firefox profile update download directory after creating webdriver 使用firefox绕过弹出窗口下载文件:Selenium Python - Download file with firefox bypassing popup : Selenium Python 这个硒firefox配置文件将文件下载到自定义文件夹有什么问题? - What is wrong with this selenium firefox profile to download file into customized folder? 使用 python selenium 下载文件,使用 firefox 驱动程序正确下载目录 - Download file with python selenium, correct download directory with firefox driver Selenium Firefox 配置文件首选项下载对话框 - Selenium Firefox profile preferences download dialogue box Python文件在浏览器窗口中打开而不是下载提示 - Python file opens up in browser window instead of download prompt
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM