【问题标题】:Why does Selenium timeout?为什么硒超时?
【发布时间】:2018-04-22 12:15:05
【问题描述】:

我正在尝试在 Visual Studio 2017 中将 Selenium.WebDriver v3.7(来自 NuGet)、Firefox ESR v52.4.1(64 位)和 geckodriver v0.19.1 结合在一起。下面是我的 非常 em> 简单的测试控制台应用程序。

public class Program
{
    public static void Main(string[] args)
    {
        FirefoxDriverService service = FirefoxDriverService.CreateDefaultService(@"C:\geckodriver\0.19.1");
        service.FirefoxBinaryPath = @"C:\Program Files\Mozilla Firefox\firefox.exe";
        IWebDriver driver = new FirefoxDriver(service);
        driver.Navigate().GoToUrl("http://news.bbc.co.uk");
    }
}

geckodriver 目录添加到我的 Windows 10 PATH 系统环境变量中。

但是,FirefoxDriver 的实例化超时。下面是堆栈跟踪(审查了一些不相关的细节):

1510233068873   geckodriver     INFO    geckodriver 0.19.1
1510233068878   geckodriver     INFO    Listening on 127.0.0.1:65327
1510233070006   mozrunner::runner       INFO    Running command: "C:\\Program Files\\Mozilla Firefox\\firefox.exe" "-marionette" "-profile" "C:\\Users\\x\\AppData\\Local\\Temp\\rust_mozprofile.qt9HYniTrrCC"

Unhandled Exception: OpenQA.Selenium.WebDriverException: The HTTP request to the remote WebDriver server for URL http://localhost:65327/session timed out after 60 seconds. ---> System.Net.WebException: The request was aborted: The operation has timed out.
   at System.Net.HttpWebRequest.GetResponse()
   at OpenQA.Selenium.Remote.HttpCommandExecutor.MakeHttpRequest(HttpRequestInfo requestInfo)
   --- End of inner exception stack trace ---
   at OpenQA.Selenium.Remote.HttpCommandExecutor.MakeHttpRequest(HttpRequestInfo requestInfo)
   at OpenQA.Selenium.Remote.HttpCommandExecutor.Execute(Command commandToExecute)
   at OpenQA.Selenium.Remote.DriverServiceCommandExecutor.Execute(Command commandToExecute)
   at OpenQA.Selenium.Remote.RemoteWebDriver.Execute(String driverCommandToExecute, Dictionary`2 parameters)
   at OpenQA.Selenium.Remote.RemoteWebDriver.StartSession(ICapabilities desiredCapabilities)
   at OpenQA.Selenium.Remote.RemoteWebDriver..ctor(ICommandExecutor commandExecutor, ICapabilities desiredCapabilities)
   at OpenQA.Selenium.Firefox.FirefoxDriver..ctor(FirefoxDriverService service)
   at Selenium3Stuff.Program.Main(String[] args) in c:\users\x\source\repos\Selenium3Stuff\Selenium3Stuff\Program.cs:line 12

谁能帮我理解发生了什么?如果我创建 FirefoxDriver 并将 UseLegacyImplementation 选项设置为 true(因此绕过 geckodriver),那么一切正常。

【问题讨论】:

  • 超时是因为浏览器无法回复驱动。这可能是因为您使用的驱动程序版本与 ESR 52 不兼容。建议的最低版本为 55。
  • oo...好信息@FlorentB。你能指出你推荐的版本信息的来源吗?
  • 查看关于 v0.19.0 版本的评论:github.com/mozilla/geckodriver/releases

标签: c# selenium selenium-webdriver selenium-firefoxdriver geckodriver


【解决方案1】:

正如@FlorentB. 所指出的,我使用的是不受支持的 Firefox 版本。我开始使用 Firefox v56.0(32 位)和 geckodriver v0.19.1(32 位),一切都很好。经验教训:注意发行说明中提到的版本。

【讨论】:

    【解决方案2】:

    错误说明了一切:

    Unhandled Exception: OpenQA.Selenium.WebDriverException: The HTTP request to the remote WebDriver server for URL http://localhost:65327/session timed out after 60 seconds. ---> System.Net.WebException: The request was aborted: The operation has timed out.
    

    此外,当您使用 UseLegacyImplementation 选项设置为 true(因此,绕过 geckodriver)时,一切正常。

    在第一种情况下,geckodriver 正在尝试监听端口 65327,该端口以某种方式被占用。因此无法完成 HTTP 请求并出现 System.Net.WebException

    解决方案是停止任何其他服务并保持端口空闲以供 GeckoDriver 监听。如果问题仍然存在,请联系System Reboot

    【讨论】:

    • 是的,我看到请求超时,但我无法确定原因。如果我手动将浏览器指向 geckodriver.exe 开始监听的任何临时端口,我会得到响应。所以我不明白它怎么可能是一个被占用的端口问题。我已经尝试了几次,每次都使用不同的临时端口。
    • 没错。 geckodriver.exe 启动时会选择任何随机端口。我们需要将Test Bed(即运行Selenium related Tests)的Test Environment与其他服务隔离,这样我们就可以避免任何不可预见的障碍,而Test ExecutionIn Progress
    • 好的,但这是我的本地机器,有一个简单的程序。我仍然不明白为什么会发生超时。
    • @Mark 检查您的系统是否有任何其他服务在任何其他端口上运行。您还可以询问您的系统管理员是否根据组织政策保留/阻止了任何端口。因此,在这种情况下,您必须释放被阻止的端口才能使您的 Selenium Tests 顺利运行。
    【解决方案3】:

    我安装了 Mozilla Firefox Browser v56.0(32 位)和以下 Nuget 包:

    能够使用驱动程序通过 Mozilla Firefox 自动打开 www.google.co.in。

    【讨论】:

      猜你喜欢
      • 2018-10-02
      • 2014-08-13
      • 1970-01-01
      • 1970-01-01
      • 2020-08-20
      • 2021-03-19
      • 2016-12-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多