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

Asked

Viewed 1,609 times

2

I’m doing an automation using Selenium with Chrome Webdriver. The application has to make a series of downloads that need to be saved with different names (Report Date + Type) and in 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);

Therefore, I cannot rename the file name or select the corresponding directory of the file I am downloading, all with the same name and in the same folder. Does anyone have any idea how I can do that?

1 answer

1


You can’t do that.

What you can do is create a temporary directory and move your files after 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);
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.