为什么我的代码无法连接到Selenium Remote Webdriver?

问题描述:

我尝试使用“Selenium Remote Webdriver”进行自动测试,但在尝试连接远程服务器时发生异常。为什么我的代码无法连接到Selenium Remote Webdriver?

唯一的例外是:

Unexpected error. System.Net.WebException: Unable to connect to the remote server ---> System.Net.Sockets.SocketException: No connection could be made because the target machine actively refused it 127.0.0.1:4444 
    at System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress) 
    at System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, Socket s4, Socket s6, Socket& socket, IPAddress& address, ConnectSocketState state, IAsyncResult asyncResult, Exception& exception) 
    --- End of inner exception stack trace --- 
    at System.Net.HttpWebRequest.GetRequestStream(TransportContext& context) 
    at System.Net.HttpWebRequest.GetRequestStream() 
    at OpenQA.Selenium.Remote.HttpCommandExecutor.Execute(Command commandToExecute) in c:\Projects\WebDriver\trunk\dotnet\src\WebDriver\Remote\HttpCommandExecutor.cs:line 109 
    at OpenQA.Selenium.Remote.RemoteWebDriver.Execute(String driverCommandToExecute, Dictionary`2 parameters) in c:\Projects\WebDriver\trunk\dotnet\src\WebDriver\Remote\RemoteWebDriver.cs:line 836 

我写的代码是:

  DesiredCapabilities capabilities = DesiredCapabilities.Firefox(); 
      capabilities.Platform = new Platform(PlatformType.Vista); 
      capabilities.SetCapability("api_key", "REPLACE_API_KEY"); 
      capabilities.SetCapability("api_secret", "REPLACE_API_SECRET"); 


      var remoteAddress = new Uri("http://122.176.112.91:4444/wd/hub"); 

      // ... but only if it supports javascript 
      capabilities.IsJavaScriptEnabled = true; 


      // Get a handle to the driver. This will throw an exception 
      // if a matching driver cannot be located 
      IWebDriver driver = new RemoteWebDriver(remoteAddress, capabilities); 
      driver.Manage().Timeouts().ImplicitlyWait(new TimeSpan(0, 0, 30)); 


      // Query the driver to find out more information 
      ICapabilities actualCapabilities = ((RemoteWebDriver)driver).Capabilities; 

请帮我弄清楚这个prolem。

在此先感谢。

+3

那么有没有硒实例在'HTTP侦听://122.176.112.91:4444/WD/hub'? – Arran 2013-03-04 11:58:28

+0

防火墙问题或许? – 2013-03-04 15:19:23

+0

尝试更改端口号。希望能帮助到你。 – Hemanth 2013-03-04 16:15:07

堆栈跟踪指示本地计算机正在主动拒绝任何连接。仔细检查防火墙允许Firefox在所有端口上使用Firefox。

不幸的是,我遇到了这个问题,即使在防火墙处于打开状态: C# + VisualStudio + Multiple WebDriver instances = SocketExceptions