我编写了一个 Java 程序,它使用 Selenium 来通过 Firefox 激活并形成连接,目前正在尝试通过 SSH 在单独的服务器上运行该程序。然而,尽管该程序在我的机器上运行良好,但当我通过 SSH 运行它时,出现错误。这是我收到的具体消息:
org.openqa.selenium.firefox.NotConnectedException: Unable to connect to host 127.0.0.1 on port 7055 after 45000 ms. Firefox console output:
Error: no display specified
Error: no display specified
at org.openqa.selenium.firefox.internal.NewProfileExtensionConnection.start(NewProfileExtensionConnection.java:118)
at org.openqa.selenium.firefox.FirefoxDriver.startClient(FirefoxDriver.java:246)
at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:115)
at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:193)
at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:186)
at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:182)
at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:95)
at GoogleTestNineIndigoVersion.main(GoogleTestNineIndigoVersion.java:278)
Exception in thread "main" org.openqa.selenium.WebDriverException: Failed to connect to binary FirefoxBinary(/usr/bin/firefox) on port 7055; process output follows:
Error: no display specified
Error: no display specified
Build info: version: '2.42.0', revision: '5e82430', time: '2014-05-22 20:18:33'
System info: host: 'video', ip: '130.63.94.246', os.name: 'Linux', os.arch: 'amd64', os.version: '2.6.32-573.3.1.el6.cse.x86_64', java.version: '1.8.0_91'
Driver info: driver.version: FirefoxDriver
at org.openqa.selenium.firefox.internal.NewProfileExtensionConnection.start(NewProfileExtensionConnection.java:130)
at org.openqa.selenium.firefox.FirefoxDriver.startClient(FirefoxDriver.java:246)
at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:115)
at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:193)
at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:186)
at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:182)
at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:95)
at GoogleTestNineIndigoVersion.main(GoogleTestNineIndigoVersion.java:278)
Caused by: org.openqa.selenium.firefox.NotConnectedException: Unable to connect to host 127.0.0.1 on port 7055 after 45000 ms. Firefox console output:
Error: no display specified
Error: no display specified
at org.openqa.selenium.firefox.internal.NewProfileExtensionConnection.start(NewProfileExtensionConnection.java:118)
... 7 more
根据我所做的研究,我发现这个问题的出现是因为,作为一般规则,任何给定版本的 Selenium 都只与当时最新版本的 Firefox 兼容。大多数遇到类似问题的人都被告知升级 Selenium。然而,由于我相当确定我拥有最新版本的 Selenium (2.53.0),因此我认为合理的解决方案是升级我尝试运行该程序的服务器上的 Firefox 版本。不幸的是,我尝试运行该程序的服务器是我大学的服务器(我创建这个程序是我暑期工作的一部分,为我的一位教授工作,他们希望我在大学的服务器上运行该程序),而且我显然没有升级我大学的 Firefox 版本所需的权限。
我能想到的唯一其他解决方案是使用旧版本的 Selenium,但事实证明这是徒劳的。当通过 SSH 运行时,Selenium 2.42.0 和 Selenium 2.51.0 都遇到了同样的问题——后者尤其让我想知道版本兼容性是否真的是问题,考虑到从我所见,Selenium 2.51.0 应该与大学服务器上的 Firefox 版本一起工作。但是,我研究了人们通过 SSH 使用 Selenium 时遇到的其他问题,并且我唯一遇到的异常(exception)情况是版本兼容性问题。
有谁知道我可以做些什么来解决这个问题?我笔记本电脑上的 Firefox 版本(适用于 Selenium 2.53.0 和 2.51.0,但在 2.42.0 上遇到与我大学服务器上的版本相同的问题)是版本 46.0.1,而我大学服务器上的版本(不适用于我尝试过的任何版本的 Selenium)是 firefox-38.6.1-1.el6.centos.x86_64 (包括更多详细信息)如果它们相关)。
最佳答案
在我当前的项目中遇到类似的问题,可能的解决方案可以在以下链接中找到https://github.com/seleniumhq/selenium-google-code-issue-archive/issues/7272
public static int browserRetryCount =
Integer.parseInt(System.getProperty("webdriver.instance.retry.count","5"));
static int browserAttempt = 1;
public WebDriver getConnection(String browserType) {
try {
browserDriver = new FirefoxDriver(capabilities);
} catch (Exception e) {
Logger.logMessage("Error while initializing Driver object: "+e.getMessage());
String bindErrorMessage = "Unable to bind to locking port";
if ((e.getMessage().contains(bindErrorMessage)) && (browserAttempt<=browserRetryCount))
{
Logger.logMessage("Attemp2: browser instance creation!");
browserAttempt++;
int newPort = 7060+new java.util.Random().nextInt(10);
Logger.logMessage("newPort="+newPort);
System.setProperty("webdriver.firefox.port", newPort+"");
getConnection(browserType);
} else {
throw new UIFailure("Not able to create a driver object",e);
}
}
}
或
你也可以尝试这个
FirefoxProfile ffProfile = new FirefoxProfile();
FirefoxBinary ffBinary = new FirefoxBinary();
ffBinary.setTimeout(TimeUnit.SECONDS.toMillis(180));
driver = new FirefoxDriver(ffBinary, ffProfile);
关于java - 通过 SSH 使用 Selenium 运行 Firefox 时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37548429/