1

当我运行 selenium 脚本时,我遇到了这个问题:(我在 tomcat 中运行这段代码,这是我遇到异常的时候,但在普通的 eclipse 中这工作得很好)

**org.openqa.selenium.WebDriverException: org.openqa.selenium.WebDriverException: Failed to connect to binary FirefoxBinary(/opt/firefox/firefox-bin) on port 7055; process output follows:** 
Error: cannot open display: www.displayName.com:0.0
�
System info: os.name: 'Linux', os.arch: 'amd64', os.version: '3.2.21-1.32.6.amzn1.x86_64', java.version: '1.7.0_05'
Driver info: driver.version: firefox
System info: os.name: 'Linux', os.arch: 'amd64', os.version: '3.2.21-1.32.6.amzn1.x86_64', java.version: '1.7.0_05'
Driver info: driver.version: firefox
    at org.openqa.selenium.firefox.internal.ExtensionConnectionFactory.connectTo(ExtensionConnectionFactory.java:46)
    at org.openqa.selenium.firefox.FirefoxDriver.connectTo(FirefoxDriver.java:127)
    at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:117)
    at com.domain.myproject.server.MyServlet.doFilter(CrawlServlet.java:120)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at com.ensarm.wikirealty.server.service.CacheControllerFilter.doFilter(CacheControllerFilter.java:46)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at com.google.inject.servlet.FilterChainInvocation.doFilter(FilterChainInvocation.java:67)
    at com.google.inject.servlet.ManagedFilterPipeline.dispatch(ManagedFilterPipeline.java:122)
    at com.google.inject.servlet.GuiceFilter.doFilter(GuiceFilter.java:110)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:225)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:169)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:927)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:999)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:565)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:307)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
    at java.lang.Thread.run(Thread.java:722)
Caused by: org.openqa.selenium.WebDriverException: Failed to connect to binary FirefoxBinary(/opt/firefox/firefox-bin) on port 7055; process output follows: 
Error: cannot open display: www.wikirealty.com:0.0
�
System info: os.name: 'Linux', os.arch: 'amd64', os.version: '3.2.21-1.32.6.amzn1.x86_64', java.version: '1.7.0_05'
Driver info: driver.version: firefox
    at org.openqa.selenium.firefox.internal.NewProfileExtensionConnection.connectToBrowser(NewProfileExtensionConnection.java:60)
    at org.openqa.selenium.firefox.internal.NewProfileExtensionConnection.<init>(NewProfileExtensionConnection.java:49)
    at org.openqa.selenium.firefox.internal.ExtensionConnectionFactory.connectTo(ExtensionConnectionFactory.java:44)
    ... 27 more
Caused by: org.openqa.selenium.firefox.NotConnectedException: Failed to start up socket within 45000
    at org.openqa.selenium.firefox.internal.AbstractExtensionConnection.connectToBrowser(AbstractExtensionConnection.java:143)
    at org.openqa.selenium.firefox.internal.NewProfileExtensionConnection.connectToBrowser(NewProfileExtensionConnection.java:58)
    ... 29 more

请任何人都可以帮助我解决这个例外并为此提供解决方案

编辑

代码:

String url = "google.com";
WebDriver driver = new FirefoxDriver();
String str =""; 
driver.get(url); 
driver.wait(10000);
str = driver.getPageSource();
System.out.println(str); 
driver.close(); 
4

2 回答 2

3

经过大量谷歌搜索并通过 Tarken 链接,最终解决方案是:

  1. 在无头服务器上添加 xvfb
  2. 在创建实例时将 xvfb 属性设置为 firefox 配置文件。
  3. 正确组合你的罐子,请移除多余的罐子
于 2012-08-30T11:09:49.650 回答
2

如果您的实例是无头的,那就是问题所在。Firefox 需要一个窗口。

一些无头问题的链接:

于 2012-08-28T11:03:23.417 回答