C# 和 Selenium WebDriver 4.0+ - Microsoft Edge 配置文件选择窗口阻止测试运行

问题描述 投票:0回答:1

当尝试使用以下代码使用 Selenium WebDriver 4.10 和 C#(.NET Framework 4.5.2)构建测试时,我收到一个弹出窗口,要求选择一个配置文件。据我了解,下面的代码已经指定了要使用的配置文件:

string strEdgeProfilePath = @"C:\\Users\\" + Environment.UserName + @"\\AppData\\Local\\Microsoft\\Edge\\User Data";
string strDefautProfilePath = strEdgeProfilePath + @"\\Default";
string strSeleniumProfilePath = strEdgeProfilePath + @"\\Selenium"; // <---- I have deleted this folder and copied the contents of the "Default" folder to no avail.

using (var service = EdgeDriverService.CreateDefaultService(@"C:\Temp\Selenium\Edge")) {
    service.UseVerboseLogging = true;
    EdgeOptions edgeOptions = new EdgeOptions();
    edgeOptions.AddArgument("--user-data-dir=" + strSeleniumProfilePath);
    edgeOptions.AddArgument("--profile-directory=Selenium");
    driver = new EdgeDriver(@"C:\Temp\Selenium\Edge", edgeOptions); // <----- msedgeserve.exe located here
}

即使我选择配置文件 Microsoft Edge 也不会继续并且测试超时。

我能做些什么来阻止配置文件选择并让测试运行?

c# selenium microsoft-edge
1个回答
0
投票

你的代码有一些问题:

  1. 你不需要使用双反斜杠,反斜杠就足够了。
  2. --user-data-dir
    的值不正确。使用
    strEdgeProfilePath
    就足够了。

所以正确的代码应该是这样的:

string strEdgeProfilePath = @"C:\Users\" + Environment.UserName + @"\AppData\Local\Microsoft\Edge\User Data";
...

// Here you set the path of the profile ending with User Data not the profile folder
edgeOptions.AddArgument("--user-data-dir=" + strEdgeProfilePath);

// Here you specify the actual profile folder
// If it is Default profile, no need to use this line of code
edgeOptions.AddArgument("--profile-directory=Selenium");

此外,为避免运行 Edge 进程错误,您需要按照 this answer 中的备份步骤进行操作。请在代码中使用备份文件夹而不是原始文件夹。

© www.soinside.com 2019 - 2024. All rights reserved.