首页 > 解决方案 > IE 模式下 Edge 的 C# Selenium 代码抛出 Selenium.WebDriverException - URL 的远程 WebDriver 服务器超时

问题描述

我必须在IE 模式下使用 Selenium C# for Edge 浏览器自动化网站。以此为参考https://stackoverflow.com/a/63556157/3725706我编写的 C# selenium 控制台应用程序能够在 IE 模式下调用 Edge 浏览器。但是,30 秒后它会抛出以下异常:

OpenQA.Selenium.WebDriverException:'对 URL http://localhost:50949/session/9e037fd8-47f5-46c6-a25f-dd1089e53864/url 的远程 WebDriver 服务器的 HTTP 请求在 30 秒后超时。

下面是示例 C# Selenium 代码:

var directory = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location.Substring(0, Assembly.GetEntryAssembly().Location.IndexOf("bin\\")));
directory = Path.Combine(directory, "webdrivers");
string DriverServiceDirectory;
if (System.Environment.Is64BitOperatingSystem)
{
   DriverServiceDirectory = Path.Combine(directory, @"IEDriverServerx64");
}
else
{
   DriverServiceDirectory = Path.Combine(directory, @"IEDriverServerWin32");
}
var iedriver = "IEDriverServer.exe";
var EdgeDriverLocalPath = @"C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe";
if (!Directory.Exists(directory) || !File.Exists(EdgeDriverLocalPath))
{
  Console.WriteLine("Failed to find {0} in {1} folder.", directory, EdgeDriverLocalPath);
  return;
}

var ieService = InternetExplorerDriverService.CreateDefaultService(DriverServiceDirectory, iedriver);
var ieOptions = new InternetExplorerOptions { };
ieOptions.AddAdditionalCapability("ie.edgechromium", true);
ieOptions.AddAdditionalCapability("ie.edgepath", EdgeDriverLocalPath);
ieOptions.IntroduceInstabilityByIgnoringProtectedModeSettings = true;
ieOptions.EnableNativeEvents = false;
ieOptions.IgnoreZoomLevel = true;
ieOptions.RequireWindowFocus = false;
ieOptions.PageLoadStrategy = PageLoadStrategy.Eager;

var webDriver = new InternetExplorerDriver(ieService, ieOptions, TimeSpan.FromSeconds(30));
Thread.Sleep(3000);
webDriver.Url = "http://www.bing.com";

Console.WriteLine("√ Open Bing and search 'Test Base for Microsoft 365'");
Thread.Sleep(3000);

IWebElement searchBox = webDriver.FindElement(By.Id("sb_form_q"));
searchBox.SendKeys("Test Base for Microsoft 365");

Console.WriteLine("√ Clicking the enter key...");

IWebElement submitButton = webDriver.FindElement(By.Id("submit"));
submitButton.SendKeys("\n");
submitButton.Click();            

Console.WriteLine("√ Search keys clicked successfully");
Thread.Sleep(5000);

if (webDriver != null)
{
   Console.WriteLine("Successfully ran the Selenium script!!!");
   webDriver.Close();
}

在本地运行代码时,它会在 IE 模式下打开 Edge 浏览器并打开 bing.com,但在这一行webDriver.Url = "http://www.bing.com";中它会引发异常,并且不会执行进一步的代码。

我还尝试将 TimeSpan 时间增加到 180 秒,但仍然抛出相同的异常。根据我对 Stack Overflow 的多次搜索,我添加了额外的 ieOptions,例如ieOptions.EnableNativeEvents = false;ieOptions.PageLoadStrategy = PageLoadStrategy.Eager;但所有结果都属于相同的异常。

任何人都可以帮助解决这个问题。以便执行测试用例。谢谢!

标签: c#seleniumselenium-webdriverselenium-iedriverselenium-edgedriver

解决方案


推荐阅读