为什么 Selenium 会超时?

Why does Selenium timeout?

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

public class Program
{
    public static void Main(string[] args)
    {
        FirefoxDriverService service = FirefoxDriverService.CreateDefaultService(@"C:\geckodriver[=10=].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目录添加到我的Windows10PATH系统环境变量。

但是,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)那么一切正常。

错误说明了一切:

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.

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

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

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