Selenium Error - 对远程WebDriver的HTTP请求在60秒后超时

Nat*_*han 72 c# teamcity selenium nunit selenium-webdriver

我已经使用Selenium好几个月,我们用它来自动化我们的一些内部测试过程.脚本一直很好.我最近使用FF 27.01升级到C#2.40.0 webdriver,我们的脚本现在在随机位置失败,出现以下错误.

[Portal.SmokeTest.SmokeRunTest.Booking] TearDown method failed. OpenQA.Selenium.WebDriverException : The HTTP request to the remote WebDriver server for URL htt(p)://localhost:7055/hub/session/56e99e88-ba17-4d12-bef1-c6a6367ccc2f/element timed out after 60 seconds.
  ----> System.Net.WebException : The operation has timed out
TearDown : OpenQA.Selenium.WebDriverException : The HTTP request to the remote WebDriver server for URL htt(p)://localhost:7055/hub/session/56e99e88-ba17-4d12-bef1-c6a6367ccc2f/window timed out after 60 seconds.
  ----> System.Net.WebException : The operation has timed out
[09:01:20]
[Portal.SmokeTest.SmokeRunTest.Booking] TearDown method failed. OpenQA.Selenium.WebDriverException : The HTTP request to the remote WebDriver server for URL htt(p)://localhost:7055/hub/session/56e99e88-ba17-4d12-bef1-c6a6367ccc2f/element timed out after 60 seconds.
  ----> System.Net.WebException : The operation has timed out
TearDown : OpenQA.Selenium.WebDriverException : The HTTP request to the remote WebDriver server for URL htt(p)://localhost:7055/hub/session/56e99e88-ba17-4d12-bef1-c6a6367ccc2f/window timed out after 60 seconds.
  ----> System.Net.WebException : The operation has timed out
   at OpenQA.Selenium.Support.UI.DefaultWait`1.PropagateExceptionIfNotIgnored(Exception e)
   at OpenQA.Selenium.Support.UI.DefaultWait`1.Until[TResult](Func`2 condition)
   at Portal.Test.Helpers.Process_Bookings.OpenBookings.SelectBooking(String bookingnumber)
   at Portal.SmokeTest.SmokeRunTest.Booking() in d:\TeamCityAgent\work\dac1dcea7f2e80df\SmokeTests\SmokeRunTest.cs:line 68
--WebException
   at System.Net.HttpWebRequest.GetResponse()
   at OpenQA.Selenium.Remote.HttpCommandExecutor.CreateResponse(WebRequest request)
--TearDown
   at OpenQA.Selenium.Remote.HttpCommandExecutor.CreateResponse(WebRequest request)
   at OpenQA.Selenium.Remote.HttpCommandExecutor.Execute(Command commandToExecute)
   at OpenQA.Selenium.Firefox.Internal.ExtensionConnection.Execute(Command commandToExecute)
   at OpenQA.Selenium.Remote.RemoteWebDriver.Execute(String driverCommandToExecute, Dictionary`2 parameters)
   at OpenQA.Selenium.Remote.RemoteWebDriver.Close()
   at Portal.Test.Helpers.Setup.CloseWebdriver()
   at Portal.SmokeTest.SmokeRunTest.TearDown() in d:\TeamCityAgent\work\dac1dcea7f2e80df\SmokeTests\SmokeRunTest.cs:line 162
--WebException
   at System.Net.HttpWebRequest.GetResponse()
   at OpenQA.Selenium.Remote.HttpCommandExecutor.CreateResponse(WebRequest request)
Run Code Online (Sandbox Code Playgroud)

我设法追踪到一行代码的最新错误:

_setup.driver.FindElement(By.XPath("//button[@class='buttonSmall lockBookingButton']")).Click();
Run Code Online (Sandbox Code Playgroud)

令人讨厌的是,试​​图修复问题是困难的,好像我在我的本地机器上运行测试,在调试它通过.另外,如果我通过构建机器上的NUNIT运行程序运行它,我正在运行测试,它也会通过.在使用Teamcity时,它似乎只是作为我们的自动构建运行过程的一部分而失败.就像我说的,这个问题已经好几个月了,并且唯一改变的是selenium webdriver套件.

我之前遇到过这个问题,在调试时,当Click()调用一行代码时,Firefox似乎锁定了,只有停止测试才能让Firefox继续运行.这里有很多建议,包括修改webdriver源码?如果可能的话,如果有其他人可以提供任何建议,我不想沿着这条路走下去.

小智 20

new FirefoxDriver(new FirefoxBinary(),new FirefoxProfile(),TimeSpan.FromSeconds(180));
Run Code Online (Sandbox Code Playgroud)

使用上面的代码行启动浏览器.它对我有用.


com*_*tc2 18

我有一个类似的问题,使用Chrome驱动程序(v2.23)/通过TeamCity运行测试.我可以通过在Chrome选项中添加"no-sandbox"标记来解决此问题:

var options = new ChromeOptions();
options.AddArgument("no-sandbox");
Run Code Online (Sandbox Code Playgroud)

我不确定FF驱动程序是否有类似的选项.根据我的理解,该问题与TeamCity在SYSTEM帐户下运行Selenium有关.

  • 我将其声明为 `var options = new ChromeOptions(); options.AddArgument("--no-sandbox");` 现在在 C# 版本 Webdriver 3.14 中工作。 (4认同)

Dan*_*les 13

我几个月前第一次遇到这个问题(也是在click()命令上),从那以后它一直是我的问题.这似乎是.NET Selenium绑定的某种问题.这个关于IE驱动程序的人的博客文章有助于解释发生的事情:

http://jimevansmusic.blogspot.com/2012/11/net-bindings-whaddaymean-no-response.html

不幸的是,这个问题似乎没有真正的解决方案.每当这个问题提交给Selenium开发人员(见这里)时,这是一个典型的响应:

我们需要一个可重现的场景,必须包含一个示例页面或指向公共站点页面的链接,以便重现问题.

如果您能够提交一致且可重复的测试用例,那么这可能非常有助于将此错误保持良好状态.

也就是说,也许你可以在此期间尝试这种解决方法.如果您尝试的HTML按钮click()具有onclick包含Javascript 的属性,请考虑使用JavascriptExecutor直接执行该代码,而不是调用该click()命令.我发现onclick直接执行Javascript允许我的一些测试通过.


小智 13

与 Firefox 有同样的问题。我用选项切换到 Chrome,从那以后一切都很好。

ChromeOptions options = new ChromeOptions();
options.AddArgument("no-sandbox");

ChromeDriver driver = new ChromeDriver(ChromeDriverService.CreateDefaultService(), options, TimeSpan.FromMinutes(3));
driver.Manage().Timeouts().PageLoad.Add(System.TimeSpan.FromSeconds(30));
Run Code Online (Sandbox Code Playgroud)

  • 我可能在这里弄错了,但最后一行似乎没有任何效果。PageLoad 本身就是一个 TimeSpan,而 TimeSpan 上的 .Add 是一个纯函数,它不会修改 PageLoad,它只是返回一个被丢弃的新 TimeSpan。 (3认同)

Bar*_*ala 5

遇到类似问题。尝试在驱动程序的构造函数中设置更多时间 - 添加例如。

var timespan = TimeSpan.FromMinutes(3);

var driver = new FirefoxDriver(binary, profile, timeSpan);
Run Code Online (Sandbox Code Playgroud)

  • 不,不是这个等待 ImplicitlyWait 与查找元素有关。当等待请求继续时,您需要更改默认(60秒)驱动程序超时(如果我没记错的话)。无论如何,您必须找到一行来设置 FF 驱动程序的构造函数并添加更多属性或更改超时。类似于:`driver = new FirefoxDriver(new FirefoxBinary(), new FirefoxProfile(您的个人资料的路径), TimeSpan.FromMinutes(3));` (4认同)
  • 对于 ChromeDriver,它看起来像 ```driver = new ChromeDriver(service, chromeDriverOptions, TimeSpan.FromMinutes(3));``` (3认同)

Mic*_*Mao 5

就我而言,我的按钮类型不是submitbutton我将其更改ClickSumbit然后一切正常。像下面这样的东西,

driver.FindElement(By.Id("btnLogin")).Click();

driver.FindElement(By.Id("btnLogin")).Submit();

顺便说一句,我已经尝试了这篇文章中的所有答案,但对我不起作用。


tur*_*kik 5

就我而言,上述答案都没有完全解决我的问题。我最终使用了 ( no-sandbox) 模式、延长超时时间 ( ) 的连接driver = new RemoteWebDriver(new Uri("http://localhost:4444/wd/hub"), capability, TimeSpan.FromMinutes(3));和页面加载超时 ( driver.Manage().Timeouts().PageLoad.Add(System.TimeSpan.FromSeconds(30));),所以现在我的代码如下所示:

    public IWebDriver GetRemoteChromeDriver(string downloadPath)
    {
        ChromeOptions chromeOptions = new ChromeOptions();
        chromeOptions.AddArguments(
            "start-maximized",
            "enable-automation",
            "--headless",
            "--no-sandbox", //this is the relevant other arguments came from solving other issues
            "--disable-infobars",
            "--disable-dev-shm-usage",
            "--disable-browser-side-navigation",
            "--disable-gpu",
            "--ignore-certificate-errors");
        capability = chromeOptions.ToCapabilities();

        SetRemoteWebDriver();
        SetImplicitlyWait();
        Thread.Sleep(TimeSpan.FromSeconds(2));
        return driver;
    }
    
    private void SetImplicitlyWait()
    {
        driver.Manage().Timeouts().PageLoad.Add(TimeSpan.FromSeconds(30));
    }


    private void SetRemoteWebDriver()
    {
        driver = new RemoteWebDriver(new Uri("http://localhost:4444/wd/hub"), capability, TimeSpan.FromMinutes(3));
    }
Run Code Online (Sandbox Code Playgroud)

但正如我提到的,上述方法都没有解决我的问题,我不断收到错误,并且多个 chromedriver.exe 和 chrome.exe 进程处于活动状态(大约 10 个 chromedriver 和大约 50 个 chrome)。

因此,我在某处读到,在处理驱动程序后,我应该等待几秒钟才能开始下一个测试,因此我添加了以下行来处理方法:

    driver?.Quit();
    driver?.Dispose();
    Thread.Sleep(3000);
Run Code Online (Sandbox Code Playgroud)

通过此睡眠修改,我不再出现超时错误,并且没有不必要地打开 chromedriver.exe 和 chrome.exe 进程。

我希望我能帮助那些像我一样长期困扰这个问题的人。


bit*_*bit 1

问题是Click()对构建环境的评估超时。您可能想深入了解Click().

另外,尝试添加重试,因为Click()有时评估需要更长的时间,具体取决于网络速度等