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

发布于 2025-01-11 05:03:58 字数 992 浏览 1 评论 0原文

当尝试使用 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 也不会继续并且测试超时。

我可以做什么来阻止配置文件选择并运行测试?

When attempting to build tests using Selenium WebDriver 4.10 and C# (.NET Framework 4.5.2) using the following code I receive a pop-up asking to select a profile. From what I understand the code below already is specifying the profile to use:

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
}

Even when I select the profile Microsoft Edge does not continue and the test times out.

What can I do to prevent the profile selection and get the test to run?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

摇划花蜜的午后 2025-01-18 05:03:58

您的代码中存在一些问题:

  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 进程错误,您需要按照 这个答案。请在代码中使用备份文件夹而不是原始文件夹。

There're some problems in your code:

  1. You don't need to use double backslash, backslash is enough.
  2. The value for --user-data-dir is not right. Using strEdgeProfilePath for it is enough.

So the right code should be like this:

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");

Besides, to avoid running Edge processes errors, you need to follow the back up steps in this answer. Please use the backup folder instead of the original folder in the code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文