Download file at custom path using Selenium WebDriver

后端 未结 2 1912
日久生厌
日久生厌 2020-12-09 23:00

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 dow

相关标签:
2条回答
  • 2020-12-09 23:15

    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);
    
    0 讨论(0)
  • 2020-12-09 23:16

    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!

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