我目前正在玩Selenium Marionette WebDriver
。在我的应用程序中,我想顺序打开多个Marionette驱动程序。基本上是这样的:
MarionetteDriver driver = new MarionetteDriver();
// do some stuff
driver.quit();
// a while later
driver = new MarionetteDriver();
// do some stuff
driver.quit();
现在我正面临这个问题,只有第一个Marionette实例可以成功启动,并且对于每个后来的尝试,我都会遇到以下异常。问题每次都会发生,并且使用的端口总是在变化,因此显然没有端口冲突。
Exception in thread "main" org.openqa.selenium.remote.UnreachableBrowserException: Could not start a new session. Possible causes are invalid address of the remote server or browser start-up failure.
Build info: version: '2.48.2', revision: '41bccdd10cf2c0560f637404c2d96164b67d9d67', time: '2015-10-09 13:08:06'
System info: host: 'qqilihq.local', ip: '192.168.1.2', os.name: 'Mac OS X', os.arch: 'x86_64', os.version: '10.11.2', java.version: '1.7.0_71'
Driver info: driver.version: MarionetteDriver
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:641)
at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:247)
at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:232)
at org.openqa.selenium.firefox.MarionetteDriver.run(MarionetteDriver.java:84)
at org.openqa.selenium.firefox.MarionetteDriver.<init>(MarionetteDriver.java:73)
at org.openqa.selenium.firefox.MarionetteDriver.<init>(MarionetteDriver.java:45)
at MyMainClass.main(MyMainClass.java:131)
Caused by: org.openqa.selenium.WebDriverException: org.apache.http.conn.HttpHostConnectException: Connect to localhost:41886 [localhost/127.0.0.1, localhost/0:0:0:0:0:0:0:1] failed: Connection refused
Build info: version: '2.48.2', revision: '41bccdd10cf2c0560f637404c2d96164b67d9d67', time: '2015-10-09 13:08:06'
System info: host: 'qqilihq.local', ip: '192.168.1.2', os.name: 'Mac OS X', os.arch: 'x86_64', os.version: '10.11.2', java.version: '1.7.0_71'
Driver info: driver.version: MarionetteDriver
at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:91)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:620)
... 6 more
Caused by: org.apache.http.conn.HttpHostConnectException: Connect to localhost:41886 [localhost/127.0.0.1, localhost/0:0:0:0:0:0:0:1] failed: Connection refused
at org.apache.http.impl.conn.DefaultHttpClientConnectionOperator.connect(DefaultHttpClientConnectionOperator.java:151)
at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.connect(PoolingHttpClientConnectionManager.java:353)
at org.apache.http.impl.execchain.MainClientExec.establishRoute(MainClientExec.java:380)
at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:236)
at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:184)
at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:88)
at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:110)
at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:184)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:71)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:55)
at org.openqa.selenium.remote.internal.ApacheHttpClient.fallBackExecute(ApacheHttpClient.java:143)
at org.openqa.selenium.remote.internal.ApacheHttpClient.execute(ApacheHttpClient.java:89)
at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:142)
at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:82)
... 7 more
Caused by: java.net.ConnectException: Connection refused
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:579)
at org.apache.http.conn.socket.PlainConnectionSocketFactory.connectSocket(PlainConnectionSocketFactory.java:74)
at org.apache.http.impl.conn.DefaultHttpClientConnectionOperator.connect(DefaultHttpClientConnectionOperator. java:134)
... 20 more
任何指示赞赏!
答案 0 :(得分:8)
在深入挖掘之后,我得出了以下结论,最终解决了我的问题:
Marionette resp。 wires
使用两个端口(请参阅wires --help
); marionette-port
和webdriver-port
:
Usage:
./wires [OPTIONS]
WebDriver to marionette proxy.
optional arguments:
-h,--help show this help message and exit
-b,--binary BINARY Path to the Firefox binary
--webdriver-host WEBDRIVER_HOST
Host to run webdriver server on
--webdriver-port WEBDRIVER_PORT
Port to run webdriver on
--marionette-port MARIONETTE_PORT
Port to run marionette on
--connect-existing Connect to an existing firefox process
同时运行多个MarionetteDrivers
时,两个端口显然必须与已运行的实例不同。但是,使用默认构造函数new MarionetteDriver()
时,marionette-port
保持不变(并且不基于某个空闲端口确定)。我们使用了GeckoDriverService.Builder
的一些解决方法(见下文)来随机选择两个随机选择的可用端口。
当前(版本2.48.2)GeckoDriverService
有一个空waitUntilAvailable()
的实现(如果WebDriver
准备好,应该检查)。偶尔,这会导致上面发布的UnreachableBrowserException
。
为了规避这些问题,我们在最后做了类似的事情:
// determine free ports for Marionette and WebDriver
final int marionettePort = PortProber.findFreePort();
final int webDriverPort = PortProber.findFreePort();
// override, as GeckoDriverService provides no direct way to set the Marionette port
GeckoDriverService.Builder builder = new GeckoDriverService.Builder() {
@Override
protected ImmutableList<String> createArgs() {
Builder<String> argsBuilder = ImmutableList.builder();
argsBuilder.addAll(super.createArgs());
argsBuilder.add(String.format("--marionette-port=%d", marionettePort));
return argsBuilder.build();
}
};
builder.usingPort(webDriverPort);
builder.usingDriverExecutable(pathToDriver);
GeckoDriverService driverService = builder.build();
try {
driverService.start();
} catch (IOException e) {
throw new IllegalStateException("Could not start the GeckoDriverService", e);
}
try {
// keep checking the WebDriver port via Socket until it's available;
// as far as I could tell, there is nothing more "high level", e.g. REST API
waitUntilReady(webDriverPort, TimeUnit.SECONDS.toMillis(30));
} catch (InterruptedException e) {
// ignore
}
return new MarionetteDriver(driverService, capabilities);
答案 1 :(得分:2)
您需要处理Marionette所需的二进制文件。你可以手动完成(自己下载二进制文件并导出适当的变量),或者你可以使用WebDriverManager自动完成。只需添加以下依赖项:
<dependency>
<groupId>io.github.bonigarcia</groupId>
<artifactId>webdrivermanager</artifactId>
<version>1.6.0</version>
</dependency>
然后,在您的代码调用中:
FirefoxDriverManager.getInstance().setup();
答案 2 :(得分:0)
当Selenium无法找到驱动程序所需的exe时,大多数情况UnreachableBrowserException
会发生。由于您还没有设置驱动程序功能,我假设以下步骤将解决您的问题。
根据Mozilla MDN link Marionette被设为DesiredCapability
DesiredCapabilities capabilities = DesiredCapabilities.firefox();
// Set Marionette on so the Grid will use this instead of normal FirefoxDriver
capabilities.setCapability("marionette", true);
WebDriver driver = new RemoteWebDriver(capabilities);
此外,Marionette可执行文件已添加到路径中(在Windows中):
将可执行文件添加到PATH
Selenium将尝试使用路径中的可执行文件。您需要使用以下内容将其添加到路径中。
最后,处理UnreachableBrowserException问题的另一个SO question。
答案 3 :(得分:0)
我有类似的问题,通过使用重写的构建器实现创建重写的MarionetteDriver实例来解决。接受的答案是更优雅的解决方案。是否有足够的时间深入到根本原因
public static class CustomBuilder extends org.openqa.selenium.firefox.GeckoDriverService.Builder {
@Override
protected GeckoDriverService createDriverService(File exe, int port, ImmutableList<String> args, ImmutableMap<String, String> environment) {
try {
return new GeckoDriverService(exe, port, args, environment) {
@Override
protected void waitUntilAvailable() throws MalformedURLException {
logger.info("Waiting until avaliable");
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
}
super.waitUntilAvailable();
logger.info("Finished waiting until avaliable");
}
};
} catch (IOException e) {
throw new WebDriverException(e);
}
}
}