使用Chrome驱动程序通过python和selenium下载指定位置的文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35331854/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Downloading a file at a specified location through python and selenium using Chrome driver
提问by Shubham Goyal
I am trying to automatically download some links through selenium's click functionality and I am using a chrome webdriverand python as the programming language. How can I select the download directorythrough the python program so that it does not get downloaded in the default Downloads directory. I found a solution for firefox but there the download dialog keeps popping up every time it clicks on the link which does not happen in Chrome.
我试图通过 selenium 的点击功能自动下载一些链接,我使用chrome webdriver和 python 作为编程语言。如何通过python程序选择下载目录,使其不会在默认下载目录中下载。我找到了 firefox 的解决方案,但每次点击 Chrome 中不会发生的链接时,下载对话框都会不断弹出。
采纳答案by Sarunas Urbelis
Update 2018:
2018 年更新:
Its not valid Chrome command line switch, see the source codeuse hoju answerbelow to set the Preferences.
它不是有效的 Chrome 命令行开关,请参阅源代码使用下面的hoju 答案来设置首选项。
Original:
原来的:
You can create a profile for chrome and define the download location for the tests. Here is an example:
您可以为 chrome 创建配置文件并定义测试的下载位置。下面是一个例子:
from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_argument("download.default_directory=C:/Downloads")
driver = webdriver.Chrome(chrome_options=options)
回答by Shubham Jain
If you are using script only on your local machine then use this solution
如果您仅在本地计算机上使用脚本,请使用此解决方案
Click on Menu -> Setting -> Show advanced settings... -> Downloads
单击菜单 -> 设置 -> 显示高级设置... -> 下载
Now uncheck
现在取消选中
Ask where to save each file before downloading
下载前询问每个文件的保存位置
Hope it will help you :)
希望它会帮助你:)
回答by hoju
I found the accepted solution didn't work, however this slight change did:
我发现接受的解决方案不起作用,但是这个轻微的变化确实:
import webdriver
chrome_options = webdriver.ChromeOptions()
prefs = {'download.default_directory' : '/path/to/dir'}
chrome_options.add_experimental_option('prefs', prefs)
driver = webdriver.Chrome(chrome_options=chrome_options)
回答by Alex Montoya
If you are using linux distribution
如果您使用的是 linux 发行版
Use this code
使用此代码
prefs = {'download.prompt_for_download': False,
'download.directory_upgrade': True,
'safebrowsing.enabled': False,
'safebrowsing.disable_download_protection': True}
options.add_argument('--headless')
options.add_experimental_option('prefs', prefs)
driver = webdriver.Chrome('chromedriver.exe', chrome_options=options)
driver.command_executor._commands["send_command"] = ("POST", '/session/$sessionId/chromium/send_command')
driver.desired_capabilities['browserName'] = 'ur mum'
params = {'cmd': 'Page.setDownloadBehavior', 'params': {'behavior': 'allow', 'downloadPath': r'C:\chickenbutt'}}
driver.execute("send_command", params)
回答by vsnahar
To provide download directory and chrome's diver executable path use the following code.
要提供下载目录和 chrome 的 diver 可执行路径,请使用以下代码。
from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_argument("download.default_directory=C:/Your_Directory")
driver = webdriver.Chrome(options=options ,executable_path='C:/chromedriver')
change the path in your code accordingly.
相应地更改代码中的路径。
回答by Rajesh Kumar Sahoo
the exact problem I also have faced while trying to do exactly same what you want to :)
我在尝试做完全相同的事情时也遇到的确切问题:)
For chrome:
对于铬:
from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_argument("--start-maximized")
prefs = {"profile.default_content_settings.popups": 0,
"download.default_directory":
r"C:\Users\user_dir\Desktop\",#IMPORTANT - ENDING SLASH V IMPORTANT
"directory_upgrade": True}
options.add_experimental_option("prefs", prefs)
browser=webdriver.Chrome(<chromdriver.exe path>, options=options)
For Firefox: follow this blog for the answer: https://srirajeshsahoo.wordpress.com
对于 Firefox:请关注此博客以获取答案:https: //srirajeshsahoo.wordpress.com
The blog says all about the pop up and download dir and how to do
该博客说明了有关弹出和下载目录以及如何操作的所有信息
回答by CodeWalker
This worked for me on Chrome v81.0.4044.138
这在 Chrome 上对我有用 v81.0.4044.138
preferences = {
"profile.default_content_settings.popups": 0,
"download.default_directory": os.getcwd() + os.path.sep,
"directory_upgrade": True
}
chrome_options.add_experimental_option('prefs', preferences)
browser = webdriver.Chrome(executable_path="/usr/bin/chromedriver", options=chrome_options)