我在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();
}
}
镀铬示例。
@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:等待驱动程序服务器启动超时。 构建信息:版本:&#39;未知&#39;,修订版:&#39;未知&#39;,时间:&#39;未知&#39; 系统信息:主机:&#39; LT9LTDRC2&#39;,ip:&#39; 192.168.1.6&#39;,os.name:&#39; Windows 10&#39;,os.arch:&#39; amd64&#39;,os.version:&#39; 10.0&#39;,java.version:&#39; 1.8.0_131&#39; 驱动程序信息:driver.version:Gecko_Driver 在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.startSession(RemoteWebDriver.java:250) 在org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:236) 在org.openqa.selenium.remote.RemoteWebDriver。(RemoteWebDriver.java:137) 在org.openqa.selenium.chrome.ChromeDriver。(ChromeDriver.java:184) 在org.openqa.selenium.chrome.ChromeDriver。(ChromeDriver.java:171) 在org.openqa.selenium.chrome.ChromeDriver。(ChromeDriver.java:124) 在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) 在org.junit.runners.model.FrameworkMethod $ 1.runReflectiveCall(FrameworkMethod.java:50) 在org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) 在org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47) 在org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) 在org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325) 在org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78) 在org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57) 在org.junit.runners.ParentRunner $ 3.run(ParentRunner.java:290) 在org.junit.runners.ParentRunner $ 1.schedule(ParentRunner.java:71) 在org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) 在org.junit.runners.ParentRunner.access $ 000(ParentRunner.java:58) 在org.junit.runners.ParentRunner $ 2.evaluate(ParentRunner.java:268) 在org.junit.runners.ParentRunner.run(ParentRunner.java:363) 在org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86) 在org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) 在org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459) 在org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678) 在org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382) 在org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192) 引起:org.openqa.selenium.net.UrlChecker $ TimeoutException:等待[http://localhost:31675/status]在20002 ms后可用时超时 在org.openqa.selenium.net.UrlChecker.waitUntilAvailable(UrlChecker.java:107) 在org.openqa.selenium.remote.service.DriverService.waitUntilAvailable(DriverService.java:190) ......还有33个 原因:com.google.common.util.concurrent.UncheckedTimeoutException:java.util.concurrent.TimeoutException 在com.google.common.util.concurrent.SimpleTimeLimiter.callWithTimeout(SimpleTimeLimiter.java:140) 在org.openqa.selenium.net.UrlChecker.waitUntilAvailable(UrlChecker.java:80) ......还有34个 引起:java.util.concurrent.TimeoutException at java.util.concurrent.FutureTask.get(Unknown Source) 在com.google.common.util.concurrent.SimpleTimeLimiter.callWithTimeout(SimpleTimeLimiter.java:128) ......还有35个
答案 0 :(得分:4)
我们的安全部门引入了一项政策,该政策阻止了对geckodriver.exe执行的访问。这是通过尝试从cmd运行来识别的。不知道为什么我没有在IDE中获得有意义的错误(被组策略阻止),我确实得到了Chrome和IE的这个错误。为了使用驱动程序,它需要保存在程序文件中,尽管这可能是我的具体情况。如果Geckodriver出现此错误,我建议您尝试从cmd执行它以查看是否存在策略问题。
答案 1 :(得分:0)
以下是您的问题的解决方案:
我在代码中没有看到任何错误,但错误堆栈跟踪HttpHostConnectException
说明了这一切。
执行以下步骤:
throws InterruptedException
,您可以考虑将其删除。C:\\your_directory
。避免将目录名称与Temp
一起使用。
虽然您已经获得了DesiredCapabilities
Class的帮助,但在启动驱动程序实例时尚未传递它。你必须这样做:
WebDriver driver = new FirefoxDriver(capabilities);
清除您的Mozilla Firefox浏览器缓存。
在测试步骤结束时,请致电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();
如果这有助于您,请告诉我。
答案 2 :(得分:0)
我在这里遇到了同样的错误。系统重新启动后,代码可以正常工作。 我的问题是我的代码最后使一些进程处于打开状态,并且它们正在使用大量RAM。 尝试运行:
pkill geckodriver
pkill firefox
pkill selenium
然后再次运行您的代码。