我目前正在玩Selenium木偶WebDriver
。在我的应用程序中,我希望按顺序打开多个木偶驱动程序。基本上是这样的:
MarionetteDriver driver = new MarionetteDriver();
// do some stuff
driver.quit();
// a while later
driver = new MarionetteDriver();
// do some stuff
driver.quit();
现在我面临的问题是,只有第一个木偶实例可以成功启动,对于以后的每次尝试,我都会得到以下异常。问题每次都会发生,并且使用的端口总是发生变化,因此显然没有端口冲突。
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
感谢任何指点!
当Selenium找不到驱动程序所需的exe时,通常会发生UnreachableBrowserExc0019
。由于您还没有设置驱动程序功能,我假设以下步骤将解决您的问题。
根据Mozilla的要求,MDN链接木偶被设置为所需的功能
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);
此外,木偶可执行文件被添加到路径(在Windows中):
将可执行文件添加到PATH
Selenium将尝试使用路径中的可执行文件。您需要使用以下命令将其添加到路径中。
最后,另一个SO问题,其中处理了UnreachableBrowserExcure问题。
您需要处理木偶所需的二进制文件。您可以手动完成(自行下载二进制文件并导出适当的变量),也可以使用WebDriverManager自动完成。只需添加以下依赖项:
<dependency>
<groupId>io.github.bonigarcia</groupId>
<artifactId>webdrivermanager</artifactId>
<version>1.6.0</version>
</dependency>
然后,在代码调用中:
FirefoxDriverManager.getInstance().setup();
在深入研究后,我得出了以下发现,最终解决了我的问题:
>
木偶<代码>导线使用两个端口(请参见导线--help
);木偶端口
和网络驱动程序端口
:
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-marionetdriver()
木偶端口保持不变(并且不是基于某个自由端口确定的)。我们为geckodriverse服务使用了一些变通方法(见下文)。生成器
始终选择两个随机选择的可用端口。
当前(版本2.48.2)geckodriverse
有一个空的waitUntilavable()
(如果WebDriver
准备就绪,应该检查它)。偶尔,这会导致上面发布的无法访问的浏览器异常。
为了规避这些问题,我们在最后做了这样的事情:
// 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);
问题内容: 在硒测试中出现以下错误 我所做的是 环境: Firefox v47.0 Webdriver 3.0.0-beta2 问题答案: 这是完全可以预期的。没有GeckoDriver(木偶)的发行版支持该类。这是Mozilla团队开发Marionette的首要任务之一。
我正在使用Firefox 47.0.1和木偶驱动程序geckodriver-v0。8.0-win32。但我遇到了无法访问的浏览器异常,无法打开。 我的代码片段如下所示: 将异常显示为:- 组织。openqa。硒。遥远的UnreachableBrowserException:无法启动新会话。可能的原因是远程服务器地址无效或浏览器启动失败。构建信息:版本:'2.53.0',版本:'35ae25b',时
问题内容: 我正在尝试使用selenium升级到升级后的firefox Web浏览器自动化。selenium似乎需要木偶驱动程序才能继续工作。我按照开发人员的指示进行操作, 下载了驱动程序 重命名为wires.exe 以下代码无法正确地将PATH设置为自定义路径。 System.Environment.SetEnvironmentVariable(“ webdriver.gecko.driver”
我试图在远程节点上运行selenium网格代码,但它总是引发异常。 集线器: 它会听192.168.1.106:4444 节点: 它将在192.168.1.132:5566收听 Java代码: 节点日志: 操作系统:Fedora 23,Firefox版本50.1.0,selenium独立服务器3.0.1
问题内容: 我需要关闭Marionette / GeckoDriver日志记录;有什么办法吗?我已经进行了很多搜索,但是没有得到正确的答案。INFO日志为: 如何关闭此日志记录? 问题答案: 尝试了以下代码,但是没有用。好像在