Deepak Yadav
Deepak Yadav

Reputation: 351

How To Fix File Download Error For Your Location With ChromeOptions in C# with Selenium

Background - I'm trying to automate a website related to data restoring. So once the user logs in he/she can select the desired file/files and once the download button is enabled the user is able to download the file.

Problem - I'm successfully able to download the file but the problem comes when I give my desired path where I want the file to be downloaded, I'm getting "Failed - Download Error" in Chrome on using ChromeOptions. I want to download the file inside my automation suite itself but currently any location that I try gives me same error.

Please note - The location is already available and file type can be anything.

Code -
Following options of code I have tried -

-- Options 1

ChromeOptions options = new ChromeOptions();
options.AddArguments("--browser.download.folderList=2");
options.AddArguments("--browser.helperApps.neverAsk.SaveToDisk=image/jpg");
options.AddArguments("--browser.download.dir="+@"C:\\Users\\Administrator\\Downloads\\NewFolder\\");
options.AddUserProfilePreference("download.default_directory",@"C:\\Users\\Administrator\\Downloads\\NewFolder\\");

localDriver = new ChromeDriver("C:\\Downloads",options)
localDriver.Manage().Window.Maximize();

-- Options 2

ChromeOptions options = new ChromeOptions();
options.AddUserProfilePreference("download.default_directory",@"C:\\Users\\Administrator\\Downloads\\NewFolder\\");
options.AddUserProfilePreference("download.prompt_for_download","false");
options.AddUserProfilePreference("disable-popup-blocking",true);

localDriver = new ChromeDriver("C:\\Downloads",options)
localDriver.Manage().Window.Maximize();

-- Options 3

ChromeOptions options = new ChromeOptions();
options.AddArguments("--browser.download.folderList=2");
options.AddArguments("--browser.helperApps.neverAsk.SaveToDisk=image/jpg");
options.AddArguments("--browser.download.dir="+@"C:\\Users\\Administrator\\Downloads\\NewFolder\\");
options.AddUserProfilePreference("download.default_directory",@"C:\\Users\\Administrator\\Downloads\\NewFolder\\");
options.AddUserProfilePreference("download.prompt_for_download","false");
options.AddUserProfilePreference("disable-popup-blocking",true);

localDriver = new ChromeDriver("C:\\Downloads",options)
localDriver.Manage().Window.Maximize();

Questions That I Have Referred - How to download CSV file through Firefox Profile in Java

how to change file download location in Webdriver while using chrome driver/firefox driver

C# Selenium ChromeOptions not setting/changing default download location

Questions -

1.) How can I ensure that file downloading is successful with ChromeOptions or FireFox Options.

2.) Is there any standard documentation for using chrome options in C#, because in many blogs these values are mentioned as string and in some it is boolean.

3.) Any other alternative approach for this problem which I can take to tackle this file downloading.

Upvotes: 0

Views: 1687

Answers (1)

Deepak Yadav
Deepak Yadav

Reputation: 351

For the solutions in C# we can use below code -

var options = new ChromeOptions();
options.AddUserProfilePreferences("download.default_directory",@"path_to_your_location");
options.AddUserProfilePreferences("download.prompt_for_download",false);
options.AddUserProfilePreferences("download.directory_upgrade",true);

Please Note :- Path_to_your_location must be complete, for e.g. you may be trying to refer to a location within your project but still use absolute path, for e.g. you may be referring to a location within your project like ".\DataDownloaded" but instead it should be C:\Users\User1\MyProject\DataDownloaded" then you would not get this error. Also please take care of the "\" and "\\" in your paths, in this option only "\" works.

Upvotes: 0

Related Questions