Set download location and name of each downloaded file - Selenium WebDriver C #

2

I'm doing an automation using Selenium with Chrome WebDriver. The application has to do a series of downloads that need to be saved with different names (Data + Report type) and folders that match the type of report I'm downloading.

The problem is that I can only set the default directory when I instantiate a new driver

var chromeOptions = new ChromeOptions();
chromeOptions.AddUserProfilePreference("download.default_directory", downloadDirectory);
chromeOptions.AddUserProfilePreference("intl.accept_languages", "nl");
chromeOptions.AddUserProfilePreference("disable-popup-blocking", "true");

IWebDriver driver = new ChromeDriver(@"location chromeDriver", chromeOptions);


driver.Navigate().GoToUrl(url);

So, I can not rename the file name or select the corresponding directory of the file I'm downloading, all of them with the same name and in the same folder. Does anyone have any idea how I can do this?

    
asked by anonymous 03.08.2017 / 16:04

1 answer

1

You can not do this.

What you can do is create a temporary directory and move your files after they have been inserted.

Using system.IO;

string downloadDirectory = @"c:\temp\MyTest.txt";

//No seu caso, o diretório 'downloadDirectoryFinal' é uma variável de acordo com teus relatórios.
if (relatorioId==1)
{
   string downloadDirectoryFinal = @"c:\Relatorio1\Nome1.txt";
}
else
{
   string downloadDirectoryFinal = @"c:\Relatorio2\Nome2.txt";
}

//Diretório temporário
if(File.Exists (downloadDirectory))
{
   //Mover o arquivo para a pasta correspondente ao relatório que deseja
   File.Move(downloadDirectory, downloadDirectoryFinal);
}
    
04.08.2017 / 00:30