I am new to selenium and i would like to download file with selenium chrome web driver in specific custom folder. In default the file is downloading in browser specified download path. Any one suggest the best solution for downloading file in custom path in C# Selenium.
New Selenium IDE This can be done by creating an instance of the FirefoxOptions class. Then with the help of the addPreference method, we have to set the browser preferences. We shall also specify the path where the file has to be downloaded with the help of the addPreference method.
The most basic way of uploading files in Selenium is using the sendKeys method. It is an inbuilt feature for file upload in Selenium. The syntax is as below: WebElement upload_file = driver.
Hope it will be help for you!!
var chromeOptions = new ChromeOptions();
chromeOptions.AddUserProfilePreference("download.default_directory", "Your_Path");
chromeOptions.AddUserProfilePreference("intl.accept_languages", "nl");
chromeOptions.AddUserProfilePreference("disable-popup-blocking", "true");
var driver = new ChromeDriver("Driver_Path", chromeOptions);
You need to hack a bit so you can download the file at a specified location. Option 1: Use a third-party tool like AutoIt which can interact with the Windows pop-up and you can specify the path using it. Option 2: Write a custom method that can do the download using the API.
var downloadDocLink = webDriver.FindElement(By.XPath("{}")).GetAttribute("onclick");
string toBeSearched = "{string}"; //this string needs to be trimmed from the url
string downloadUrl = downloadDocLink.Substring(downloadDocLink.IndexOf(toBeSearched) + toBeSearched.Length);
var data = webDriver.DownloadByApiCall(downloadUrl);
var fileName = webDriver.FindElement(By.XPath("{Xpath}")).Text;
//Save result of report api call to file
var val = ConfigurationManager.AppSettings["OutputPath"];
var path = Environment.ExpandEnvironmentVariables(val);
var filePath = Path.Combine(path, fileName);
var dir = Path.GetDirectoryName(path);
Console.WriteLine($"Saving file with {data.Length} bytes to {path}.");
if (!Directory.Exists(dir))
Directory.CreateDirectory(dir);
File.WriteAllBytes(filePath, data);
//Ensure file was downloaded
var exists = webDriver.FileExistsSpinWait(filePath);
Assert.IsTrue(exists, $"The downloaded report is not present in the download folder: \n{filePath}");
//Remove file and ensure deleted
File.Delete(filePath);
Assert.IsFalse(File.Exists(filePath));
Helper method to download by API call
public static byte[] DownloadByApiCall(this IWebDriver driver, string apiCall)
{
var uri = new Uri(driver.Url);
var path = $"{url}/{apiCall}";
byte[] data = null;
try
{
var webRequest = (HttpWebRequest)WebRequest.Create(path);
webRequest.CookieContainer = new CookieContainer();
foreach (var cookie in driver.Manage().Cookies.AllCookies)
webRequest.CookieContainer.Add(new System.Net.Cookie(cookie.Name, cookie.Value, cookie.Path, string.IsNullOrWhiteSpace(cookie.Domain) ? uri.Host : cookie.Domain));
var webResponse = (HttpWebResponse)webRequest.GetResponse();
var ms = new MemoryStream();
var responseStream = webResponse.GetResponseStream();
responseStream.CopyTo(ms);
data = ms.ToArray();
responseStream.Close();
webResponse.Close();
}
catch (WebException webex)
{
var errResp = webex.Response;
using (var respStream = errResp.GetResponseStream())
{
var reader = new StreamReader(respStream);
Assert.Fail($"Error getting file from the server({webex.Status} - {webex.Message}): {reader.ReadToEnd()}.");
}
}
return data;
}
This works for me, it asserts if the download was successful and it's rerunnable as well as we're deleting the file in the end. Hope this helps!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With