简体   繁体   English

如何将属性设置为ChromeOptions类?

[英]How can I set the properties to ChromeOptions class?

I am writing a script in Selenium WebDriver using C#. 我正在使用C#在Selenium WebDriver中编写脚本。 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. 我正在使用ChromeOptions类及其方法来完成任务。 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. 但是,由于路径是动态的,因此我想在函数中间设置ChromeOptions类的属性。 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. 现在,当我在函数中间或在运行时更新ChromeOptions时,它将创建另一个ChromeDriver实例并打开另一个chrome窗口。 It does not update the properties of ChromeOptions class. 它不会更新ChromeOptions类的属性。 I did some experiments like removing the path of chromedriver.exe from IChromeDriver class but it started giving the following error: 我做了一些实验,例如从IChromeDriver类中删除chromedriver.exe的路径,但它开始出现以下错误:

The chromedriver.exe file does not exist in the current directory or in a directory on the PATH environment variable. chromedriver.exe文件在当前目录或PATH环境变量上的目录中不存在。

What could be the way to set the ChromeOptions in the middle of the code without creating an another instance of a IWebDriver Class? 在代码中间设置ChromeOptions而不创建IWebDriver类的另一个实例的方法是什么?

You can only set ChromeOptions, and thus the download path, via the class constructor(s). 您只能通过类构造函数设置ChromeOptions,从而设置下载路径。 There is no property you can update once you've instantiated ChromeDriver. 实例化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. 为此,我要做的就是检查Chrome中的“在下载前询问将文件保存在哪里”设置,然后在测试中与“另存为”对话框提示进行交互,输入完整的动态保存文件路径并单击“保存”。 The problem is that this is a Windows dialog and Selenium cannot interact with it. 问题是这是Windows对话框,Selenium无法与其交互。 I am using MS CodedUI to work with it. 我正在使用MS CodedUI来使用它。 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}"); 我在与对话框的“保存”按钮进行交互时遇到困难,因此我使用Keyboard.SendKeys("{ENTER}"); You may have to add some {TAB} s in there. 您可能需要在其中添加一些{TAB}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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