通过 python 和 selenium 使用 Chrome 驱动程序在指定位置下载文件

Downloading a file at a specified location through python and selenium using Chrome driver

我正在尝试通过 selenium 的点击功能自动下载一些 links,我正在使用 chrome webdriver 和 python 作为编程语言。 如何通过 python 程序 select 下载目录 使其不被下载到默认的下载目录中。我找到了 firefox 的解决方案,但每次单击 link 时下载对话框都会弹出,这在 Chrome.

中不会发生

这是没有 chrome profiling/options 设置的非代码级解决方案。

如果您仅在本地计算机上使用脚本,请使用此解决方案

点击菜单 -> 设置 -> 显示高级设置... -> 下载

现在取消勾选

下载前询问每个文件的保存位置

希望对您有所帮助:)

2018 年更新:

无效 Chrome 命令行开关,请参阅下面的 source code use 设置首选项。

Original:

您可以为 chrome 创建配置文件并定义测试的下载位置。这是一个例子:

from selenium import webdriver

options = webdriver.ChromeOptions() 
options.add_argument("download.default_directory=C:/Downloads")

driver = webdriver.Chrome(chrome_options=options)

我发现接受的解决方案不起作用,但这个微小的变化起作用了:

from selenium 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)

如果您使用 linux 分发

使用此代码

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)

要提供下载目录和chrome的驱动程序可执行路径,请使用以下代码。

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')

相应地更改代码中的路径。

我在尝试做与您完全相同的事情时也遇到过的确切问题:)

对于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)

对于 Firefox: 关注此博客以获得答案: https://srirajeshsahoo.wordpress.com/2018/07/26/how-to-bypass-pop-up-during-download-in-firefox-using-selenium-and-python/

该博客详细介绍了弹出窗口和下载目录以及操作方法

这对我有用 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)

使用首选项解决了我的问题

path = os.path.dirname(os.path.abspath(__file__))
prefs = {"download.default_directory":path}
options = Options()
options.add_experimental_option("prefs", prefs)
driver = webdriver.Chrome('../harveston/chromedriver.exe',options = options)

我看到很多人有同样的问题,只是在最后添加反斜杠

op = webdriver.ChromeOptions()
prefs = {'download.default_directory' : 'C:\Users\SAJComputer\PycharmProjects\robot-test\'}
op.add_experimental_option('prefs', prefs)
driver = webdriver.Chrome(executable_path=driver_path , options=op)

下面的代码片段适用于 Windows/linux/MacOs 发行版:

    downloadDir = f"{os.getcwd()}//downloads//"
    # Make sure path exists.
    Path(downloadDir).mkdir(parents=True, exist_ok=True)
    
    # Set Preferences.
    preferences = {"download.default_directory": downloadDir,
                   "download.prompt_for_download": False,
                   "directory_upgrade": True,
                   "safebrowsing.enabled": True}

    chromeOptions = webdriver.ChromeOptions()
    chromeOptions.add_argument("--window-size=1480x560")
    chromeOptions.add_experimental_option("prefs", preferences)

    driver = webdriver.Chrome(DRIVER_PATH, options=chromeOptions)
    driver.get(url)
    time.sleep(10)
    driver.close()