我正在尝试在CentOS-VM上设置测试环境。
我在Eclipse中运行的Java-maven项目中有以下三个文件:
main.java
package main;
public class main {
public static void main(String[] args) {
StartFirefox fire = new StartFirefox();
StartChrome chrome = new StartChrome();
chrome.start();
fire.start();
}
}
StartFirefox.java
package main;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class StartFirefox {
WebDriver fire;
public StartFirefox() {
System.setProperty("webdriver.gecko.driver", "src/main/resources/geckodriver/geckodriver");
System.out.println("debug 1");
fire = new FirefoxDriver();
System.out.println("debug 2");
fire.manage().window().maximize();
}
public void start() {
System.out.println("debug 3");
fire.get("https://www.google.com/");
System.out.println("debug 4");
}
}
StartChrome.java
package main;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class StartChrome {
WebDriver chrome;
public StartChrome() {
String pathToChromeDriver = "src/main/resources/chromedriver/chromedriver";
System.setProperty("webdriver.chrome.driver", pathToChromeDriver);
System.out.println("debug 1");
chrome = new ChromeDriver();
System.out.println("debug 2");
}
public void start() {
System.out.println("debug 3");
chrome.get("https://www.google.com/");
System.out.println("debug 4");
}
}
当使用Firefox运行程序时,窗口会打开,但它不会转到我的网址。 相反,我在一段时间后在控制台中得到以下输出(似乎是超时):
debug 1
1516891060262 geckodriver INFO geckodriver 0.19.1
1516891060265 geckodriver INFO Listening on 127.0.0.1:32675
1516891060856 mozrunner::runner INFO Running command: "/usr/bin/firefox" "-marionette" "-profile" "/tmp/rust_mozprofile.v4slcGFX6wrp"
1516891061783 Marionette INFO Listening on port 2828
Exception in thread "main" org.openqa.selenium.WebDriverException: connection refused
Build info: version: '3.8.1', revision: '6e95a6684b', time: '2017-12-01T19:05:32.194Z'
System info: host: 'localhost.localdomain', ip: '127.0.0.1', os.name: 'Linux', os.arch: 'amd64', os.version: '3.10.0-693.11.6.el7.x86_64', java.version: '1.8.0_152'
Driver info: driver.version: FirefoxDriver
remote stacktrace: stack backtrace:
0: 0x4edb3c - backtrace::backtrace::trace::hc4bd56a2f176de7e
1: 0x4edb72 - backtrace::capture::Backtrace::new::he3b2a15d39027c46
2: 0x440ac8 - webdriver::error::WebDriverError::new::ha0fbd6d1a1131b43
3: 0x447ba9 - geckodriver::marionette::MarionetteHandler::create_connection::hf0532ddb9e159684
4: 0x428570 - <webdriver::server::Dispatcher<T, U>>::run::h2119c674d7b88193
5: 0x4029b9 - std::sys_common::backtrace::__rust_begin_short_backtrace::h21d98a9ff86d4c25
6: 0x40be65 - std::panicking::try::do_call::h5cff0c9b18cfdbba
7: 0x5e6a6c - panic_unwind::__rust_maybe_catch_panic
at /checkout/src/libpanic_unwind/lib.rs:99
8: 0x41eb22 - <F as alloc::boxed::FnBox<A>>::call_box::h413eb1d9d9f1c473
9: 0x5df13b - alloc::boxed::{{impl}}::call_once<(),()>
at /checkout/src/liballoc/boxed.rs:692
- std::sys_common::thread::start_thread
at /checkout/src/libstd/sys_common/thread.rs:21
- std::sys::imp::thread::{{impl}}::new::thread_start
at /checkout/src/libstd/sys/unix/thread.rs:84
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at org.openqa.selenium.remote.W3CHandshakeResponse.lambda$new$0(W3CHandshakeResponse.java:57)
at org.openqa.selenium.remote.W3CHandshakeResponse.lambda$getResponseFunction$2(W3CHandshakeResponse.java:104)
at org.openqa.selenium.remote.ProtocolHandshake.lambda$createSession$0(ProtocolHandshake.java:123)
at java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:193)
at java.util.Spliterators$ArraySpliterator.tryAdvance(Spliterators.java:958)
at java.util.stream.ReferencePipeline.forEachWithCancel(ReferencePipeline.java:126)
at java.util.stream.AbstractPipeline.copyIntoWithCancel(AbstractPipeline.java:498)
at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:485)
at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471)
at java.util.stream.FindOps$FindOp.evaluateSequential(FindOps.java:152)
at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
at java.util.stream.ReferencePipeline.findFirst(ReferencePipeline.java:464)
at org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHandshake.java:126)
at org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHandshake.java:73)
at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:142)
at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:83)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:601)
at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:219)
at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:142)
at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:120)
at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:98)
at main.StartFirefox.<init>(StartFirefox.java:12)
at main.main.main(main.java:6)
我被困在这里因为我不知道如何解决这个问题。可能是什么原因?
使用Chrome运行程序时,窗口会打开,但它不会转到我的网址。 相反,我在控制台中得到以下输出:
debug 1
Starting ChromeDriver (v2.9.248304) on port 5186
[0.899][WARNING]: PAC support disabled because there is no system implementation
Jan 25, 2018 3:40:23 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: OSS
debug 2
debug 3
Exception in thread "main" org.openqa.selenium.WebDriverException: org.apache.http.conn.HttpHostConnectException: Connect to localhost:5186 [localhost/127.0.0.1, localhost/0:0:0:0:0:0:0:1] failed: Connection refused (Connection refused)
Build info: version: '3.8.1', revision: '6e95a6684b', time: '2017-12-01T19:05:32.194Z'
System info: host: 'localhost.localdomain', ip: '127.0.0.1', os.name: 'Linux', os.arch: 'amd64', os.version: '3.10.0-693.11.6.el7.x86_64', java.version: '1.8.0_152'
Driver info: driver.version: RemoteWebDriver
at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:92)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:601)
at org.openqa.selenium.remote.RemoteWebDriver.get(RemoteWebDriver.java:325)
at main.StartChrome.start(StartChrome.java:18)
at main.main.main(main.java:9)
Caused by: org.apache.http.conn.HttpHostConnectException: Connect to localhost:5186 [localhost/127.0.0.1, localhost/0:0:0:0:0:0:0:1] failed: Connection refused (Connection refused)
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:156)
at org.openqa.selenium.remote.internal.ApacheHttpClient.execute(ApacheHttpClient.java:86)
at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:161)
at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:83)
... 4 more
Caused by: java.net.ConnectException: Connection refused (Connection refused)
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:589)
at org.apache.http.conn.socket.PlainConnectionSocketFactory.connectSocket(PlainConnectionSocketFactory.java:75)
at org.apache.http.impl.conn.DefaultHttpClientConnectionOperator.connect(DefaultHttpClientConnectionOperator.java:142)
... 17 more
在Windows 10上运行此项目时,一切正常。
所以这似乎是操作系统的一个问题。但我在搜索时找不到任何解决方案。它只是说服务器和无头版本。
更新:
添加了调试消息。 Firefox只会显示消息1,而Chrome会打印到消息3。
答案 0 :(得分:0)
来自geckodriver 0.19.1的第一个错误堆栈跟踪确实给了我们一些提示:
org.openqa.selenium.WebDriverException:连接被拒绝
但ChromeDriver的第二个堆栈跟踪(v2.9.248304)说明了一切:
org.openqa.selenium.WebDriverException:org.apache.http.conn.HttpHostConnectException:连接到localhost:8004 [localhost / 127.0.0.1,localhost / 0:0:0:0:0:0:0:1 ]失败:连接被拒绝(连接被拒绝)
/ etc / hosts文件中的条目未正确配置。
要解决此问题,请确保Linux Box上的/etc/hosts
包含以下条目:
127.0.0.1 localhost.localdomain localhost