简体   繁体   中英

Unable to download file using ChromeDriver

I am using chromedriver with selenium to download files from application. But while clicking on download button in application, it given error as " Failed-Download error. "

Chromedriver version : 2.21 Selenium version : 2.53.0

Code for initializing chrome driver and changing download location :

            String newPath = "D:\\Backup" + File.separator + "Database ";
            new File(newPath).mkdir();
            HashMap<String, Object> chromePrefs = new HashMap<String, Object>();
            chromePrefs.put("profile.default_content_settings.popups", 0);
            chromePrefs.put("download.default_directory", newPath);
            chromePrefs.put("safebrowsing.enabled", "true");
            ChromeOptions options = new ChromeOptions();
            options.setExperimentalOption("prefs", chromePrefs);
            options.addArguments("--test-type");
            DesiredCapabilities cap = DesiredCapabilities.chrome();
            cap.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
            cap.setCapability("disable-popup-blocking", true);
            cap.setCapability(ChromeOptions.CAPABILITY, options);
            System.setProperty("webdriver.chrome.driver", CHROME_DRIVER_PATH);
            driver = new ChromeDriver(cap);
            // Maximize the driver window
            driver.manage().window().maximize();

Error is :

在此处输入图片说明

Can someone help me with this? I am able to download file from Chrome manually.

In my case I had something similar, but the error was in the folder name I used. I describe the path as C:/myFolder instead of C:\\myFolder.

In previous version of ChromeDriver the first approach was still valid. Now it looks like this is not working anymore giving some Download error.

A Failed - Download error is displayed when the provided folder is missing or inaccessible. It could be the case here since I noticed an extra space at the end which is probably stripped once the folder is created. Try this way instead:

String newPath = Path.Combine("D:\\Backup", "Database");
if (!Directory.Exists(newPath)){
    newPath = Directory.CreateDirectory(newPath).FullName;
}

I had a similar problem where i was trying to change the file download directory path at run-time before downloading. Below code worked for me.

//Set Browser Capabilities.
String ProjectDirectory=RunConfiguration.getProjectDir()
String DownloadFolderPath1=ProjectDirectory+"/Downloads"
String DownloadFolderPath=DownloadFolderPath1.replace('/', '\\')
String AppURL=GlobalVariable.MyAppURL

Map<String, Object> chromePrefs = new HashMap<String, Object>()
chromePrefs.put("profile.default_content_settings.popups", 0);
chromePrefs.put("download.default_directory", DownloadFolderPath)
chromePrefs.put("download.prompt_for_download", false)
chromePrefs.put("plugins.plugins_disabled", "Chrome PDF Viewer");
ChromeOptions options=new ChromeOptions();

//options.addArguments("--headless")
//options.addArguments("--window-size=1920,1080")
options.addArguments("--test-type")
//options.addArguments("--disable-gpu")
options.addArguments("--no-sandbox")
//options.addArguments("--disable-dev-shm-usage")
options.addArguments("--disable-software-rasterizer")
options.addArguments("--disable-popup-blocking")
options.addArguments("--disable-extensions")
options.setExperimentalOption("prefs", chromePrefs)

DesiredCapabilities cap = DesiredCapabilities.chrome()
cap.setCapability(ChromeOptions.CAPABILITY, options)
cap.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true)
System.setProperty("webdriver.chrome.driver", DriverFactory.getChromeDriverPath())
WebDriver driver = new ChromeDriver(cap);       
driver.get(AppURL)
driver.manage().window().maximize()

It took some time to find out my error, but it is very simple to fix. In my case, I should not use relative filepath names for the download folder, instead, I had to use absolute filepath names.

If you are on Linux and "download.default_directory" still hold your personal directory value, watch env XDG_DOWNLOAD_DIR in file ~/.config/user-dirs.dirs

You can remove variable form that file or, you can set it to whatever you like before running your program.

My SW set:

  • Ubuntu bionic, 18.04.5 LTS
  • chromedriver.86.0.4240.22.lin64
  • Python 3.9
  • selenium 3.141.0
  • splinter 0.14.0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM