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

后端 未结 8 1489
囚心锁ツ
囚心锁ツ 2020-12-01 02:56

I am trying to automatically download some links through selenium\'s click functionality and I am using a chrome webdriver and python as the programming lan

相关标签:
8条回答
  • 2020-12-01 03:27

    Update 2018:

    Its not valid Chrome command line switch, see the source code use hoju answer below to set the Preferences.

    Original:

    You can create a profile for chrome and define the download location for the tests. Here is an example:

    from selenium import webdriver
    
    options = webdriver.ChromeOptions() 
    options.add_argument("download.default_directory=C:/Downloads")
    
    driver = webdriver.Chrome(chrome_options=options)
    
    0 讨论(0)
  • 2020-12-01 03:34

    This is non code level solution with no chrome profiling/options settings.

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

    0 讨论(0)
  • 2020-12-01 03:35

    This worked for me on 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)
    
    0 讨论(0)
  • 2020-12-01 03:35

    If you are using linux distribution

    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)
    
    0 讨论(0)
  • 2020-12-01 03:36

    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

    The blog says all about the pop up and download dir and how to do

    0 讨论(0)
  • 2020-12-01 03:37

    I found the accepted solution didn't work, however this slight change did:

    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)
    
    0 讨论(0)
提交回复
热议问题