Selenium Webdriver in Python - files download directory change in Chrome preferences

前端 未结 6 1545
逝去的感伤
逝去的感伤 2020-11-27 05:09

I\'m using Selenium Webdriver (in Python) to automate the downloading of thousands of files. I want to set Chrome\'s download folder programmatically. After reading this, I

相关标签:
6条回答
  • 2020-11-27 05:24

    I think you also need

    "directory_upgrade": true
    

    Using the dictionary directly in a Chrome 'Prefrences' file, on a local windows install of chrome Version 28.0.1500.95 m, with the following download options:

       "download": {
          "default_directory": "C:\\Users\\rdub\\Desktop",
          "extensions_to_open": ""
       },
    

    I get the default location, versus the desktop. When I change it to this:

       "download": {
          "default_directory": "C:\\Users\\rdub\\Desktop",
          "directory_upgrade": true,
          "extensions_to_open": ""
       },
    

    I get the desktop location.

    Try the following:

    desired_caps = {'prefs': {'download': {'default_directory': '/Users/thiagomarzagao/Desktop/downloaded_files/', "directory_upgrade": true, "extensions_to_open": ""}}}
    
    0 讨论(0)
  • 2020-11-27 05:39

    Pass the variable to "download.default_directory"

    Store the dir path in variable and pass the variable to "download.default_directory"

    Note: Both .py file and Folder "PDF_Folder" in same location and file should download in Folder "PDF_Folder"

    exepath = sys.arg[0]
    # get the path from the .py file
    Dir_path = os.path.dirname(os.path.abspath(exepath))
    # get the path of "PDF_Folder" directory
    Download_dir = Dir_path+"\\PDF_Folder\\"
    
    preferences = {"download.default_directory": Download_dir , # pass the variable
                       "download.prompt_for_download": False,
                       "directory_upgrade": True,
                       "safebrowsing.enabled": True }
    chrome_options.add_experimental_option("prefs", preferences)
    driver = webdriver.Chrome(chrome_options=chrome_options,executable_path=r'/pathTo/chromedriver')
    driver.get("urlfiletodownload");
    
    0 讨论(0)
  • 2020-11-27 05:41

    The following worked for me:

    chromeOptions = webdriver.ChromeOptions()
    prefs = {"download.default_directory" : "/some/path"}
    chromeOptions.add_experimental_option("prefs",prefs)
    chromedriver = "path/to/chromedriver.exe"
    driver = webdriver.Chrome(executable_path=chromedriver, chrome_options=chromeOptions)
    

    Source: https://sites.google.com/a/chromium.org/chromedriver/capabilities

    0 讨论(0)
  • 2020-11-27 05:43

    If anyone is still having trouble and the above solutions didn't work, I found adding a following slash ('\') to my download path.

    Mine looked like this:

        if browser == 'chrome':
            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)
            return webdriver.Chrome(executable_path=Base.chromedriver_dir, chrome_options=options)
    
    0 讨论(0)
  • 2020-11-27 05:46

    I try all the anwsers in this question, but it doesn't work for my in Ubuntu 16.10. So I add the change with os.environ for the variable XDG_DOWNLOAD_DIR. Which doesn't work, but I think that it helps.

    That is:

    os.environ['XDG_DOWNLOAD_DIR'] = default_download_directory
    

    The really change that works perfectly for me is setup the download folder via the command xdg-user-dirs-update through a system call in execution time:

    os.system("xdg-user-dirs-update --set DOWNLOAD " + default_download_directory)
    

    So, all my code related to setup the download dir is the following:

    import os
    from selenium import webdriver
    
    default_download_directory = str(os.path.dirname(os.path.abspath(__file__))) + "/download"
    
    os.environ['XDG_DOWNLOAD_DIR'] = default_download_directory
    
    os.system("xdg-user-dirs-update --set DOWNLOAD " + default_download_directory)
    
    desired_caps = {
        'prefs': {
                'download': {
                    'default_directory': str(os.path.dirname(os.path.abspath(__file__))) + "/download", 
                    "directory_upgrade": "true", 
                    "extensions_to_open": ""
                    }
                  }
            }
    
    options = webdriver.ChromeOptions() 
    options.add_argument("download.default_directory=" + str(os.path.dirname(os.path.abspath(__file__))) + "/download")
    
    browser = webdriver.Chrome(chrome_options=options, desired_capabilities=desired_caps)
    
    0 讨论(0)
  • 2020-11-27 05:47

    For anybody still wondering why their implementation doesn't work: You have to put the FULL PATH for it to work. e.g. '/Users/you/dlfolder' won't work, while 'C:/Users/you/dlfolder' will.

    0 讨论(0)
提交回复
热议问题