简体   繁体   中英

How can I set the properties to ChromeOptions class?

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM