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?