Java Selenium Webdriver 连接被拒绝

Java Selenium Webdriver Connection Refused

我在 selenium webdriver 上遇到了非常常见的连接被拒绝错误。几周前正在执行相同的代码。

我一直在循环阅读现有帖子,并尝试更新 geckodriver 和 FireFox,但无济于事。我可以 运行 在另一台计算机上使用相同的代码 运行 使用相同版本的驱动程序、浏览器和库等。如何找到特定于这台机器的原因?错误如下。

调试 1 调试 2 调试 3

Exception in thread "main" org.openqa.selenium.WebDriverException: org.apache.http.conn.HttpHostConnectException: Connect to localhost:28379 [localhost/127.0.0.1, localhost/0:0:0:0:0:0:0:1] failed: Connection refused: connect Build info: version: 'unknown', revision: 'unknown', time: 'unknown' System info: host: 'LT9LTDRC2', ip: '10.130.3.15', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_131' Driver info: driver.version: Gecko_Driver  
at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:91)  
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:637)     
at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:250)    
at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:236)    
at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:137)  
at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:191)     at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:108)     at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:137)     at seleniumPrograms.Gecko_Driver.main(Gecko_Driver.java:13) 
Caused by: org.apache.http.conn.HttpHostConnectException: Connect to localhost:28379 [localhost/127.0.0.1, localhost/0:0:0:0:0:0:0:1] failed: Connection refused: connect   
at org.apache.http.impl.conn.DefaultHttpClientConnectionOperator.connect(DefaultHttpClientConnectionOperator.java:159)  
at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.connect(PoolingHttpClientConnectionManager.java:359)    
at org.apache.http.impl.execchain.MainClientExec.establishRoute(MainClientExec.java:381)    
at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:237)   at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:185)   at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:89)  
at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:111)   at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:185)    
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:72)     
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:56)     
at org.openqa.selenium.remote.internal.ApacheHttpClient.fallBackExecute(ApacheHttpClient.java:139)  
at org.openqa.selenium.remote.internal.ApacheHttpClient.execute(ApacheHttpClient.java:87)   
at org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHandshake.java:343)   
at org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHandshake.java:159)   
at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:142)     
at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:82)  ... 8 more 
Caused by: java.net.ConnectException: Connection refused: connect   
at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method)  
at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source) 
    at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)   
at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)    
at java.net.AbstractPlainSocketImpl.connect(Unknown Source)     
at java.net.PlainSocketImpl.connect(Unknown Source)     
at java.net.SocksSocketImpl.connect(Unknown Source)     
at java.net.Socket.connect(Unknown Source)  
at org.apache.http.conn.socket.PlainConnectionSocketFactory.connectSocket(PlainConnectionSocketFactory.java:75)     
at org.apache.http.impl.conn.DefaultHttpClientConnectionOperator.connect(DefaultHttpClientConnectionOperator.java:142)  ... 23 more

我得到了这个 运行甚至下面的基本代码。

enter code here
package seleniumPrograms;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.firefox.FirefoxDriver;
    import org.openqa.selenium.remote.DesiredCapabilities;

    public class Gecko_Driver {
        public static void main(String[] args)  {
             System.out.println("Debug 1");
             DesiredCapabilities capabilities=DesiredCapabilities.firefox();
             System.out.println("Debug 2");
             capabilities.setCapability("marionette", true);
             System.out.println("Debug 3");
             WebDriver driver = new FirefoxDriver(capabilities);
             System.out.println("Debug 4");
             driver.get("http://www.google.com");

             driver.manage().window().maximize();  
             driver.quit();
        }
 }

示例chrome。

@Test
        public void testGoogleSearch() throws InterruptedException {
          // Optional, if not specified, WebDriver will search your path for chromedriver.
          System.setProperty("webdriver.chrome.driver", "C:\chromedriver_win32\chromedriver.exe");

          WebDriver driver = new ChromeDriver();
          driver.get("http://www.google.com/xhtml");
          Thread.sleep(5000);  // Let the user actually see something!
          WebElement searchBox = driver.findElement(By.name("q"));
          searchBox.sendKeys("ChromeDriver");
          searchBox.submit();
          Thread.sleep(5000);  // Let the user actually see something!
          driver.quit();
        }

失败痕迹:

org.openqa.selenium.WebDriverException: Timed out waiting for driver server to start. Build info: version: 'unknown', revision: 'unknown', time: 'unknown' System info: host: 'LT9LTDRC2', ip: '192.168.1.6', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_131' Driver info: driver.version: Gecko_Driver at org.openqa.selenium.remote.service.DriverService.waitUntilAvailable(DriverService.java:193) at org.openqa.selenium.remote.service.DriverService.start(DriverService.java:181) at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:78) at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:637) at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:250) at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:236) at org.openqa.selenium.remote.RemoteWebDriver.(RemoteWebDriver.java:137) at org.openqa.selenium.chrome.ChromeDriver.(ChromeDriver.java:184) at org.openqa.selenium.chrome.ChromeDriver.(ChromeDriver.java:171) at org.openqa.selenium.chrome.ChromeDriver.(ChromeDriver.java:124) at seleniumPrograms.Gecko_Driver.testGoogleSearch(Gecko_Driver.java:16) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at org.junit.runners.model.FrameworkMethod.runReflectiveCall(FrameworkMethod.java:50) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57) at org.junit.runners.ParentRunner.run(ParentRunner.java:290) at org.junit.runners.ParentRunner.schedule(ParentRunner.java:71) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) at org.junit.runners.ParentRunner.access[=14=]0(ParentRunner.java:58) at org.junit.runners.ParentRunner.evaluate(ParentRunner.java:268) at org.junit.runners.ParentRunner.run(ParentRunner.java:363) at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86) at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192) Caused by: org.openqa.selenium.net.UrlChecker$TimeoutException: Timed out waiting for [http://localhost:31675/status] to be available after 20002 ms at org.openqa.selenium.net.UrlChecker.waitUntilAvailable(UrlChecker.java:107) at org.openqa.selenium.remote.service.DriverService.waitUntilAvailable(DriverService.java:190) ... 33 more Caused by: com.google.common.util.concurrent.UncheckedTimeoutException: java.util.concurrent.TimeoutException at com.google.common.util.concurrent.SimpleTimeLimiter.callWithTimeout(SimpleTimeLimiter.java:140) at org.openqa.selenium.net.UrlChecker.waitUntilAvailable(UrlChecker.java:80) ... 34 more Caused by: java.util.concurrent.TimeoutException at java.util.concurrent.FutureTask.get(Unknown Source) at com.google.common.util.concurrent.SimpleTimeLimiter.callWithTimeout(SimpleTimeLimiter.java:128) ... 35 more

这是您问题的解决方案:

我在你的代码中没有看到任何错误,但错误堆栈跟踪 HttpHostConnectException 说明了一切。

执行以下步骤:

  1. 我觉得没有必要使用throws InterruptedException,你可以考虑去掉它。
  2. 根据最佳做法,尝试将 geckodriver 放在方便的位置:例如C:\your_directory.
  3. 避免将目录名称与 Temp.

  4. 一起使用
  5. 虽然你用了DesiredCapabilitiesClass的帮助,但是你启动驱动实例的时候还没有通过。你一定在做:

    WebDriver driver = new FirefoxDriver(capabilities);
    
  6. 清除您的 Mozilla Firefox 浏览器缓存。

  7. 从 Windows 任务管理器中,手动终止 运行ning geckodriver.exe
  8. 的所有实例
  9. 您可能需要 运行 CCleaner 来阻止所有不需要的东西。
  10. 重启你的机器。
  11. 执行你的测试。它应该很好用。
  12. 在测试步骤结束时调用 driver.quit() 以防止 geckodriver 实例悬空。

  13. 这是您自己的工作代码块,添加了 System.setProperty 行:

    System.setProperty("webdriver.gecko.driver", "C:\your_directory\geckodriver.exe");
    System.out.println("Debug 1");
    DesiredCapabilities capabilities=DesiredCapabilities.firefox();
    System.out.println("Debug 2");
    capabilities.setCapability("marionette", true);
    System.out.println("Debug 3");
    WebDriver driver = new FirefoxDriver(capabilities);
    System.out.println("Debug 4");
    driver.get("http://www.google.com");
    driver.manage().window().maximize();  
    driver.quit();
    

如果这对你有帮助,请告诉我。

我们的安全部门引入了一项政策,该政策阻止了对 geckodriver.exe 执行的访问。这是通过从 cmd 尝试 运行 来识别的。不知道为什么我没有在 gecko 的 IDE(被组策略阻止)中得到有意义的错误,我确实在 chrome 和 IE 中得到这个错误。为了使用驱动程序,它需要保存在程序文件中,尽管这可能特定于我的情况。如果您收到 Geckodriver 的此错误,我建议您尝试从 cmd 执行它以查看是否存在政策问题。

我在这里遇到了同样的错误。系统重启后,代码工作正常。 我的问题是我的代码在最后留下了一些进程,并且它们使用了大量的 RAM。 尝试 运行宁:

pkill geckodriver
pkill firefox
pkill selenium

然后 运行 再次输入您的代码。