Java Selenium Webdriver Connection Refused

穿精又带淫゛_ 提交于 2019-12-18 07:08:37

问题


I am getting the all too common connection refused error on my selenium webdriver. The same code was executing a few weeks ago.

I have been reading in circles through existing posts and have tried updating geckodriver and FireFox to no avail. I can run the same code on another computer running the same versions of the driver, browser and libraries etc. How can I find the cause specific to this machine? the error is below.

Debug 1 Debug 2 Debug 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

And I get this running even the following basic code.

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();
        }
 }

Example with 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();
        }

Failure trace:

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$1.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$3.run(ParentRunner.java:290) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) at org.junit.runners.ParentRunner$2.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


回答1:


Our security dept introduced a policy which blocked access to the execution of the geckodriver.exe. This was identified by attempting to run from cmd. Not sure why I didn't get the meaningful error in the IDE (blocked by group policy) for gecko, I did get this error for chrome and IE. In order to use the driver it needed to be saved in Program files though this may be specific to my situation. If you get this error for Geckodriver i would suggest trying to execute it from cmd to see if there is a policy issue.




回答2:


Here is the solution to your Question:

I don't see any error as such in your code but the error stack trace HttpHostConnectException says it all.

Perform the following steps:

  1. I don't see any necessity to use throws InterruptedException, you may consider to remove it.
  2. As per best practices try to keep the geckodriver in convenient locations: e.g. C:\\your_directory.
  3. Avoid using directory names with Temp.

  4. Though you have taken help of DesiredCapabilities Class, you haven't passed it while you initiated the driver instance. You must be doing:

    WebDriver driver = new FirefoxDriver(capabilities);
    
  5. Clear your Mozilla Firefox Browser cache.

  6. From the Windows Task Manager, manually kill all the running instances of geckodriver.exe
  7. You may require to run the CCleaner to keep off all the unwanted stuff.
  8. Restart you machine.
  9. Execute your Test. It should work well.
  10. At the end of your Test Step do call driver.quit() to prevent geckodriver instances from dangling.

  11. Here is your own working code block adding the System.setProperty line:

    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();
    

Let me know if this helps you.



来源:https://stackoverflow.com/questions/44060582/java-selenium-webdriver-connection-refused

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!