3

I am writing automated tests using Selenium. I want to set the download directory in Edge so that I can download files as part of my test. There is an EdgeOptions object that I can provide when creating the EdgeDriver, but I don't know what to set on the EdgeOptions.

I know the equivalent of how to do this in Chrome

chromeOptions.AddUserProfilePreference("download.default_directory", @"C:\temp")

and Firefox

firefoxOptions.SetPreference("browser.download.dir", @"C:\temp")

But, how do I do the same thing in Edge? And get it to download automatically without a save prompt?

Matthew van Boheemen
  • 1,087
  • 3
  • 13
  • 21
  • I think from webdriver it is not possible. see this - https://stackoverflow.com/questions/50007004/how-to-spcify-the-download-location-for-ie11-and-edge-browsers-using-selenium-bi – Prany Jun 29 '18 at 03:56
  • You can do it via regkey: https://answers.microsoft.com/en-us/edge/forum/edge_other-edge_win10/change-default-download-location-in-edge/74677e11-1f32-41ec-b825-da0ac0a52215 – Jeremy Thompson Jun 29 '18 at 05:33

2 Answers2

1

You can do this for Edge like this:

// hide driver Console? true/false 
EdgeDriverService service = EdgeDriverService.CreateDefaultService();
service.HideCommandPromptWindow = true; // hide Console

// change Standard-Download-Path
EdgeOptions options = new EdgeOptions();
var downloadDirectory = "C:\temp";

// Setting custom download directory
options.AddUserProfilePreference("download.default_directory", downloadDirectory);

// start Selenium Driver:
webdriver = new EdgeDriver(service, options);

// max. Window
webdriver.Manage().Window.Maximize();
Dragon
  • 49
  • 7
0

As @Prany already mentioned, probably there is no way to set download automatically. And if I right understood, you want to handle with native window dialogue, when you are clicking on download button. Selenium cannot interact with native windows, but you can use this framework. The sample code would be like this:

// Press the A Key Down
KeyboardSimulator.KeyDown(Keys.A);

// Let the A Key back up
KeyboardSimulator.KeyUp(Keys.A);

// Press A down, and let up (same as two above)
KeyboardSimulator.KeyPress(Keys.A);

// Simulate (Ctrl + C) shortcut, which is copy for most applications
KeyboardSimulator.SimulateStandardShortcut(StandardShortcut.Copy);

// This does the same as above
KeyboardSimulator.KeyDown(Keys.Control);
KeyboardSimulator.KeyPress(Keys.C);
KeyboardSimulator.KeyUp(Keys.Control);

So you can simulate Ctrl + V keyboard action and Enter action. Hope it helps.

Andrei Suvorkov
  • 5,559
  • 5
  • 22
  • 48