0

I am writing a script in Selenium WebDriver using C#. In the script, I am downloading some documents from the webpage and I want to download it in a dynamic path. I am using ChromeOptions class and its method to accomplish the task. Here is my sample code:

ChromeOptions options = new ChromeOptions();
options.AddUserProfilePreference("download.default_directory", "C:\Users\Desktop\MyDownloads");
IWebDriver driver = new ChromeDriver(@"C:\Users\chromedriver_win32\" , options);

If I am using the above code in the starting of the function then it works fine.

However, I want to set the properties of ChromeOptions class in the middle of the function because my path is dynamic. Hence I just change the hard coded path with the string variable and put the following code in the middle of the function

ChromeOptions options = new ChromeOptions();
options.AddUserProfilePreference("download.default_directory", strDownloadFinalPath);
IWebDriver driver = new ChromeDriver(@"C:\Users\chromedriver_win32\" , options);

Now, when I am updating the ChromeOptions in the middle of the function or at run time, then Its creating another instance of a ChromeDriver and its opening one more chrome window. It does not update the properties of ChromeOptions class. I did some experiments like removing the path of chromedriver.exe from IChromeDriver class but it started giving the following error:

The chromedriver.exe file does not exist in the current directory or in a directory on the PATH environment variable.

What could be the way to set the ChromeOptions in the middle of the code without creating an another instance of a IWebDriver Class?

Ashish Sharma
  • 131
  • 1
  • 5
  • 19

1 Answers1

1

You can only set ChromeOptions, and thus the download path, via the class constructor(s). There is no property you can update once you've instantiated ChromeDriver. So the answer to your final question ("without creating another instance") is, you cannot.

What I have done to deal with this is to check the "Ask where to save each file before downloading" setting in Chrome and then I interact with the Save As dialog prompt in my test inputing the full dynamic save file path and clicking save. The problem is that this is a Windows dialog and Selenium cannot interact with it. I am using MS CodedUI to work with it. My dialog class for the Save As prompt:

using Microsoft.VisualStudio.TestTools.UITesting.WinControls;

public class WindowsDialogBoxView : WinWindow
{
    public WindowsDialogBoxView()
    {
        this.SearchProperties[WinWindow.PropertyNames.ClassName] = "#32770";
    }

    public WinEdit FilenameEdit
    {
        get
        {
            this.filenameEdit = new WinEdit(this);
            this.filenameEdit.SearchProperties[WinEdit.PropertyNames.Name] = "File name:";
            return this.filenameEdit;
        }
    }
    private WinEdit filenameEdit;

Usage:

WindowsDialogBoxView WindowsDialogBox = new WindowsDialogBoxView();
Keyboard.SendKeys(WindowsDialogBox.FilenameEdit, "C:\\myFileSavePath\\Blah\\FileToSave.abc");

I had difficulty interacting with the Save button of the dialog so I use Keyboard.SendKeys("{ENTER}"); You may have to add some {TAB}s in there.

jibbs
  • 742
  • 1
  • 8
  • 17
  • Your suggestion is good but my requirement is something in which the proposed solution won't suit. The files I am downloading needs to be saved in different different directories. Ex. In total of 67 files, 7 files will be stored in one folder, few in others and few on different. Do you have some other suggestions to handle my problem? I would be really greatful. – Ashish Sharma Nov 01 '16 at 16:19
  • Per my solution you can save the files wherever you want. Since you would get the Save As dialog you can dynamically enter (by the second parameter of SendKeys) whatever file path you want into the dialog instead of being forced to save to the hard coded path defined in ChromeOptions. – jibbs Nov 01 '16 at 17:10
  • I did a trick and able to achieve the desired result. I used 'WhindowHandels' class to achieve the result. Whenever, any new instance is created, I closed the old instance and copy the properties of old window and switch to the new window. Moreover, Can you please help me in running the chrome browser in minimize mode? Is there any way to run it background? – Ashish Sharma Nov 04 '16 at 12:45