Java Selenium Webdriver 连接被拒绝
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44060582/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java Selenium Webdriver Connection Refused
提问by bowja
I am getting the all too common connection refused error on my selenium webdriver. The same code was executing a few weeks ago.
我在 selenium webdriver 上遇到了非常常见的连接被拒绝错误。几周前执行了相同的代码。
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.
我一直在阅读现有帖子,并尝试更新 geckodriver 和 FireFox 无济于事。我可以在另一台运行相同版本的驱动程序、浏览器和库等的计算机上运行相同的代码。如何找到特定于这台机器的原因?错误如下。
Debug 1 Debug 2 Debug 3
调试 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
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
org.openqa.selenium.WebDriverException:等待驱动程序服务器启动超时。构建信息:版本:'未知',修订:'未知',时间:'未知'系统信息:主机:'LT9LTDRC2',ip:'192.168.1.6',os.name:'Windows 10',os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_131' 驱动程序信息:driver.version: Gecko_Driver at org.openqa.selenium.remote.service.DriverService.waitUntilAvailable(DriverService.java:193)在 org.openqa.selenium.remote.service.DriverService.start(DriverService.java:181) 在 org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:78) 在 org.openqa.selenium.remote .RemoteWebDriver.execute(RemoteWebDriver.java:637) 在 org.openqa.selenium.remote.RemoteWebDriver。http://localhost:31675/status]在 org.openqa.selenium.net.UrlChecker.waitUntilAvailable(UrlChecker.java:107) 在 org.openqa.selenium.remote.service.DriverService.waitUntilAvailable() 20002 毫秒后可用DriverService.java:190) ... 33 多个原因: 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 其他原因:java.util.concurrent.TimeoutException at java.util.concurrent.FutureTask.get(Unknown来源)在 com.google.common.util.concurrent.SimpleTimeLimiter.callWithTimeout(SimpleTimeLimiter.java:128) ... 35 更多
采纳答案by bowja
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.
我们的安全部门引入了阻止访问 geckodriver.exe 执行的策略。这是通过尝试从 cmd 运行来识别的。不知道为什么我在 IDE 中没有收到针对 Gecko 的有意义的错误(被组策略阻止),我确实在 chrome 和 IE 中收到了这个错误。为了使用驱动程序,它需要保存在程序文件中,尽管这可能特定于我的情况。如果 Geckodriver 出现此错误,我建议尝试从 cmd 执行它以查看是否存在策略问题。
回答by DebanjanB
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.
我在您的代码中没有看到任何错误,但错误堆栈跟踪HttpHostConnectException
说明了一切。
Perform the following steps:
执行以下步骤:
- I don't see any necessity to use
throws InterruptedException
, you may consider to remove it. - As per best practices try to keep the geckodriver in convenient locations: e.g.
C:\\your_directory
. Avoid using directory names with
Temp
.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);
Clear your Mozilla Firefox Browser cache.
- From the Windows Task Manager, manually kill all the running instances of geckodriver.exe
- You may require to run the CCleaner to keep off all the unwanted stuff.
- Restart you machine.
- Execute your Test. It should work well.
At the end of your Test Step do call
driver.quit()
to prevent geckodriver instances from dangling.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();
- 我认为没有必要使用
throws InterruptedException
,您可以考虑将其删除。 - 根据最佳实践,尽量将 geckodriver 放在方便的位置:例如
C:\\your_directory
. 避免使用带有
Temp
.尽管您已经获得了
DesiredCapabilities
Class 的帮助,但是您在启动驱动程序实例时并没有通过它。你一定在做:WebDriver driver = new FirefoxDriver(capabilities);
清除您的 Mozilla Firefox 浏览器缓存。
- 从 Windows 任务管理器中,手动终止所有正在运行的 geckodriver.exe 实例
- 您可能需要运行 CCleaner 来阻止所有不需要的东西。
- 重启你的机器。
- 执行您的测试。它应该工作得很好。
在测试步骤结束时调用
driver.quit()
以防止 geckodriver 实例悬空。这是您自己的工作代码块,添加了
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();
Let me know if this helps you.
如果这对您有帮助,请告诉我。