Selenium错误:在http://localhost:7055的网址上没有服务器响应

15

我正在使用Selenium、C#和NUnit编写测试,有时会出现以下错误:

OpenQA.Selenium.WebDriverException: 无法从URL httр://localhost:7055/hub/session/8dd13f5c-7ca6-4aa6-babc-f0ff6d940f0a/element 获取服务器响应

下面是堆栈跟踪信息:

OpenQA.Selenium.WebDriverException: 无法从URL httр://localhost:7055/hub/session/8dd13f5c-7ca6-4aa6-babc-f0ff6d940f0a/element 获取服务器响应 at OpenQA.Selenium.Remote.HttpCommandExecutor.CreateResponse(WebRequest request) in c:\Projects\WebDriver\trunk\dotnet\src\WebDriver\Remote\HttpCommandExecutor.cs:line 115 at OpenQA.Selenium.Remote.HttpCommandExecutor.Execute(Command commandToExecute) in c:\Projects\WebDriver\trunk\dotnet\src\WebDriver\Remote\HttpCommandExecutor.cs:line 96 at OpenQA.Selenium.Firefox.Internal.ExtensionConnection.Execute(Command commandToExecute) in c:\Projects\WebDriver\trunk\dotnet\src\WebDriver\Firefox\Internal\ExtensionConnection.cs:line 128 at OpenQA.Selenium.Remote.RemoteWebDriver.Execute(String driverCommandToExecute, Dictionary`2 parameters) in c:\Projects\WebDriver\trunk\dotnet\src\WebDriver\Remote\RemoteWebDriver.cs:line 795 at OpenQA.Selenium.Remote.RemoteWebDriver.FindElement(String mechanism, String value) in c:\Projects\WebDriver\trunk\dotnet\src\WebDriver\Remote\RemoteWebDriver.cs:line 836 at OpenQA.Selenium.Remote.RemoteWebDriver.FindElementById(String id) in c:\Projects\WebDriver\trunk\dotnet\src\WebDriver\Remote\RemoteWebDriver.cs:line 431 at OpenQA.Selenium.By.<>c__DisplayClass2.b__0(ISearchContext context) in c:\Projects\WebDriver\trunk\dotnet\src\WebDriver\By.cs:line 102 at OpenQA.Selenium.By.FindElement(ISearchContext context) in c:\Projects\WebDriver\trunk\dotnet\src\WebDriver\By.cs:line 272 at OpenQA.Selenium.Remote.RemoteWebDriver.FindElement(By by) in c:\Projects\WebDriver\trunk\dotnet\src\WebDriver\Remote\RemoteWebDriver.cs:line 289 at

.####.##.#####.#########.#####.ShareServicesTest.CreateShareWidget()

出现此错误的代码如下:

Driver.SwitchTo().Frame(frameElement);    
var wait = new WebDriverWait(parentContextable.Context.Driver, Timeout);
IWebElement element = wait.Until(d => Driver.FindElement(By.TagName(Tags.Body))); // error in this line

我发现这个错误只能在 Selenium 实现代码的这些行中抛出:

private static Response CreateResponse(WebRequest request)
        {
            Response commandResponse = new Response();

            HttpWebResponse webResponse = null;
            try
            {
                webResponse = (HttpWebResponse)request.GetResponse();
            }
            catch (WebException ex)
            {
                webResponse = (HttpWebResponse)ex.Response;
            }

            if (webResponse == null)
            {
                throw new WebDriverException("No response from server for url " + request.RequestUri.AbsoluteUri);
            }
            else
            { ...

可能有人知道为什么request.GetResponse()返回null,或者如果它失败了,为什么ex.Response也是null呢?

(2012年9月9日) 对不起,也许我没有提供所有的信息。我正在使用FireFox驱动程序、Win7和Selenium 2.25.1。我的测试只有有时会失败。在调试模式下,测试失败的次数比发布模式少。

这里我找到了可能导致这种情况发生的原因。如果我编写这样的代码:

Driver.SwitchTo().Frame(frameElement);    
var wait = new WebDriverWait(parentContextable.Context.Driver, Timeout);
Tread.Sleep(250);
IWebElement element = wait.Until(d => Driver.FindElement(By.TagName(Tags.Body)));

测试运行正常。但是Thread.Sleep不是一个好的解决方案。也许有人能够找到另一个稍微更好的解决方案。


检查您的Windows防火墙设置? - rein
只是想知道,您是否尝试切换到不同的选择器?只是想知道是否仅在使用特定选择器时出现错误(有时)。 - Arran
3个回答


1
你在使用IE浏览器吗?我之前也遇到过这个问题,直到我发现可以通过对象的配置来设置基本URL。否则,它会将你带到Selenium生成的某个页面,似乎会错过“真实”请求的提示。
 var options = new InternetExplorerOptions()
 {
     InitialBrowserUrl = _baseUrl,
     IntroduceInstabilityByIgnoringProtectedModeSettings = true
 };

 _driver = new InternetExplorerDriver(_ieDriverServerPath, options);

我在我的测试中使用TextFixtureSetup方法。


InitialBrowserUrl属性与IE驱动程序“缺失”无关。它只是一个小助手,用于帮助用户在浏览器受到过度限制的情况下无法设置保护模式设置时使用。如果您能够在您的环境中设置IE的保护模式属性,那么您应该这样做。这篇博客文章介绍了为什么需要这个hack的历史原因。此博客文章详细阐述了相关内容。 - JimEvans
谢谢你的见解,吉姆,从内部人士那里获得一些背景信息总是很棒的。但是对我来说,受保护模式设置并不起作用,并且在 OP 文本(或错误)中也没有反映出来。这并不改变一个事实,即如果您尝试在不设置此选项的情况下执行请求,则可能会遇到上述问题(就像我一样)。初始请求永远不会被测试脚本看到,您会收到“无响应”文本。 - MisterJames

0

我曾经遇到过同样的问题,并通过以下方式解决:

a)避免使用“do with retry”等方法来操作IWebElements,因为这样测试需要运行太长时间,是不必要的,而且测试会间歇性失败。

b)将Firefox版本降级到5(也许从FF 3.6到6都可以正常工作,但是新版本的FF会抛出间歇性异常,如“未收到来自hub/session的响应...”

c)如果您需要在测试中处理通过Ajax加载的元素,请确保提供一个js函数,您可以停止元素加载,因此您应该在FindElement之前从WebDdriver调用此函数并执行您想要的操作。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接