selenium - 对 URL 的远程 WebDriver 服务器的 HTTP 请求...在 60 秒后超时
问题描述
我将 Selenium 与 Internet Explorer Web 驱动程序 (IEDriverServer) 一起使用。出于某种原因,我找不到在那里打开这个错误的代码库。因此,如果有人也能指出我的方向,我将不胜感激。
这个问题似乎广泛分布在所有驱动程序中,这表明存在基本 Selenium 问题。但是 Selenium已经否认这是他们的问题。目前,人们似乎已经使用了相当广泛的黑客来克服持续存在的问题。
SO上的一个人似乎有类似的问题,建议通过增加超时来克服这个问题,这对我来说听起来是个可怕的想法,因为这只会减慢我的整体测试速度。
我得到了这些例外:
消息:对 URL http://localhost:24478/session/07896235-84ea-465e-a361-cb0ef5885ef2/url的远程 WebDriver 服务器的 HTTP 请求 在 60 秒后超时。StackTrace:在 OpenQA.Selenium.Remote.HttpCommandExecutor.MakeHttpRequest(HttpRequestInfo requestInfo) 在 OpenQA.Selenium.Remote.HttpCommandExecutor.Execute(Command commandToExecute) 在 OpenQA.Selenium.Remote.DriverServiceCommandExecutor.Execute(Command commandToExecute) 在 OpenQA.Selenium.Remote .RemoteWebDriver.Execute(String driverCommandToExecute, Dictionary`2 参数) 在 OpenQA.Selenium.Remote.RemoteWebDriver.get_Url()
和
消息:对 URL http://localhost:24478/session/07896235-84ea-465e-a361-cb0ef5885ef2/window/rect的远程 WebDriver 服务器的 HTTP 请求 在 60 秒后超时。StackTrace:在 OpenQA.Selenium.Remote.HttpCommandExecutor.MakeHttpRequest(HttpRequestInfo requestInfo) 在 OpenQA.Selenium.Remote.HttpCommandExecutor.Execute(Command commandToExecute) 在 OpenQA.Selenium.Remote.DriverServiceCommandExecutor.Execute(Command commandToExecute) 在 OpenQA.Selenium.Remote .RemoteWebDriver.Execute(String driverCommandToExecute, Dictionary`2 参数) 在 OpenQA.Selenium.Remote.RemoteWindow.get_Position()
我在给司机的几个不同的随机电话中得到了这个。就像我尝试获取浏览器的当前 URL,或者当我尝试单击浏览器中的元素时一样。
到目前为止,在我目前的测试中,我的测试似乎能够自行恢复并继续进行,因此仍会采取未来的行动。我还有一些我要尝试的技巧,但是需要几天的时间来实施和测试它们,从而给出问题的随机性。
我将 Nuget Selenium.WebDriver 包 v3.141.0 与 IEDriverServer v3.8 一起使用。由于驱动程序的另一个已知问题,我从 v3.9 回滚。
有谁知道这个问题的修复方法,或者没有它的 IE 驱动程序版本?
这是我第一次推出 Selenium。到目前为止,我一直在使用 CodedUI,它运行良好,但自从微软宣布停止使用它以来,我一直试图将 Selenium 产品作为替代产品上线。到目前为止,我已经克服了 Selenium 的大部分缺陷,让自己恢复了类似 CodedUI 的功能,希望这是剩下的最后一个问题。
这是我启动驱动程序的基本调用:
/*
* Startup the correct Selenium browser driver.
*/
_Service = InternetExplorerDriverService.CreateDefaultService(seleniumPath);
var options = new InternetExplorerOptions()
{
// Mouse clicking takes a long time using NativeEvents, so trying turning it off
EnableNativeEvents = false
};
_Browser = new InternetExplorerDriver((InternetExplorerDriverService)_Service, options);
_Browser.Manage().Timeouts().PageLoad = new TimeSpan(0, 5, 0); // wait for 5 minutes
解决方案
我创建了一些通用的重试方法。这是对 Selenium 限制的一种破解。Selenium 有一些内置的超时,可以并且应该在适当的地方使用,但是并非所有对驱动程序的调用似乎都尊重这些超时。此外,并非所有驱动程序通信问题都是 Selenium 在未收到回复后超时导致的。如果问题是由网络或权限问题引起的,那么这些方法根本无济于事。
Selenium 中用于 PageLoad、Script 和 ImplicitWait 的主要超时应该用于修复特定于这些区域的超时问题。
这些方法(从另一个来源修改)修复了一组非常狭窄的问题,即 Selenium 在调用过程中失去与 Web 驱动程序的连接,或者当它超时并且您没有其他方法可以延长超时期限时。它们通过启动对驱动程序的新调用来工作,在某些情况下,这可能会导致在浏览器中多次调用该操作,因此请谨慎使用它们。
/// <summary>
/// These retry methods are necessary because Selenium is incapable of handling timeouts
/// inside it's own system when it temporarily loses connection to the Driver.
/// Called like:
/// var return = RetryWebDriverServiceCall(f => object.method(param));
/// var return = RetryWebDriverServiceCall(f => object.attribute);
/// </summary>
/// <param name="serviceMethod"></param>
public delegate void VoidAction(params object[] oArgs);
public void RetryWebDriverServiceCall(VoidAction serviceMethod)
{
for (var loop = 0; loop < 3; loop++)
{
try
{
serviceMethod();
break;
}
catch (WebDriverException ex) // (WebDriverTimeoutException ex)
{
if (!ex.Message.Contains("timed out after 60 seconds") || loop >= 2)
throw new Exception($"UI Retry #: {loop}", ex);
System.Threading.Thread.Sleep(500);
}
}
}
public delegate T ParamsAction<T>(params object[] oArgs);
public T RetryWebDriverServiceCall<T>(ParamsAction<T> serviceMethod)
{
for (var loop = 0; loop < 3; loop++)
{
try
{
return serviceMethod();
}
catch (WebDriverException ex)
{
if (!ex.Message.Contains("timed out after 60 seconds") || loop >= 2)
throw new Exception($"UI Retry #: {loop}", ex);
}
}
throw new Exception("RetryWebDriverServiceCall failed");
}
推荐阅读
- python - 仅当辅助值不为空或 nan 时才更新主值
- r - R根据另一个数据框的完全匹配替换列的值
- asp.net - .NET Framework 4.7 与 .NET Core 3.0 中 HTTP 请求的不同性能
- python - 如果文件以 _ 开头,则无法使用 xml.etree.ElementTree 解析 xml 文件
- amazon-web-services - 如何将物联网数据存储到 Amazon RDS MySQL 数据库中?
- python - PyCharm可以增加终端的最大线宽吗?
- python - 如何找到使用 Gmail API 获取电子邮件的附件 ID?
- c++ - 在 C++ 中对字符串的字符进行反向迭代
- react-native - 无效的 Redirect_uri 对于使用本机反应的 instagram 登录
- webpack - How to host Webpack DevServer in a custom path