Define download directory for chromedriver selenium with python

前端 未结 3 2005
[愿得一人]
[愿得一人] 2021-02-04 15:54

Everything is in the title!

Is there a way to define the download directory for selenium-chromedriver used with python?

In spite of many research, I haven\'t fou

相关标签:
3条回答
  • 2021-02-04 16:21

    Please try the below code....

    System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver");
    String downloadFilepath = "/path/to/download";
    HashMap<String, Object> chromePrefs = new HashMap<String, Object>();
    chromePrefs.put("profile.default_content_settings.popups", 0);
    chromePrefs.put("download.default_directory", downloadFilepath);
    ChromeOptions options = new ChromeOptions();
    HashMap<String, Object> chromeOptionsMap = new HashMap<String, Object>();
    options.setExperimentalOptions("prefs", chromePrefs);
    options.addArguments("--test-type");
    DesiredCapabilities cap = DesiredCapabilities.chrome();
    cap.setCapability(ChromeOptions.CAPABILITY, chromeOptionsMap);
    cap.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
    cap.setCapability(ChromeOptions.CAPABILITY, options);
    WebDriver driver = new ChromeDriver(cap);
    
    0 讨论(0)
  • 2021-02-04 16:29

    For chromedriver1 create a new profile, and inside that profile set download.default_directory to the desired location, and set this profile for chrome using chrome.profile. The selenium-chromedriver package should have some methods for creating new profiles (at least it does with ruby), as they need some special handling.

    Chromedriver2 doesn't support setting the profile. You can set preferences with it. If you want to set the download directory this is how you do it:

    prefs: { download: { default_directory: "/tmp" } }
    

    The ruby selenium-webdriver doesn't support this feature yet, the python variant might do however.

    0 讨论(0)
  • 2021-02-04 16:42

    I have faced recently the same issue. Tried a lot of solutions found in the Internet, no one helped. So finally I came to this:

    • Launch chrome with empty user-data-dir (in /tmp folder) to let chrome initialize it
    • Quit chrome
    • Modify Default/Preferences in newly created user-data-dir, add those fields to the root object (just an example):

      "download": { "default_directory": "/tmp/tmpX7EADC.downloads", "directory_upgrade": true }

    • Launch chrome again with the same user-data-dir

    Now it works just fine.

    Another tip: If you don't know file name of file that is going to be downloaded, create snapshot (list of files) of downloads directory, then download the file and find its name by comparin snapshot and current list of files in the downloads directory.

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