【问题标题】:org.openqa.selenium.WebDriverException: connection refused while using Selenium Java and webdriver (linux headless with Xvfb)org.openqa.selenium.WebDriverException:使用 Selenium Java 和 webdriver 时连接被拒绝(Linux headless with Xvfb)
【发布时间】:2018-10-23 04:44:58
【问题描述】:

最近我得到了一个新的虚拟服务器,我从几天前开始尝试配置它,以使我的基于 Java (8) 的 selenium web 应用程序运行。这个 webapp 在 tomcat (8.5.31) 中运行,并使用 selenium 进行一些基本的网站抓取。服务器的操作系统是gentoo(在我有一个运行良好的debian系统之前)

我已经花了几天时间让它工作,但现在我陷入了以下错误:

1526203737341   geckodriver     INFO    geckodriver 0.20.1
1526203737367   geckodriver     INFO    Listening on 127.0.0.1:25016
1526203737796   mozrunner::runner       INFO    Running command: "/usr/bin/firefox-bin" "-marionette" "-profile" "/tmp/rust_mozprofile.ESuH8rdqpwT8"
1526203737802   geckodriver::marionette DEBUG   Waiting 60s to connect to browser
1526203739872   Marionette      INFO    Listening on port 2828
1526203797867   webdriver::server       DEBUG   <- 500 Internal Server Error {"value":{"error":"unknown error","message":"connection refused","stacktrace":""}}
11:29:57.944 [pool-2-thread-1] ERROR test.Test - Exception occured while processing.
org.openqa.selenium.WebDriverException: connection refused
Build info: version: '3.12.0', revision: '7c6e0b3', time: '2018-05-08T14:04:26.12Z'
System info: host: 'gentoo', ip: 'xxx.xxx.xxx.xxx', os.name: 'Linux', os.arch: 'amd64', os.version: '4.9.95-gentoo', java.version: '1.8.0_172'
Driver info: driver.version: FirefoxDriver

这是进程列表显示的内容:

root      7808     1  0 14:36 ?        00:00:00   /usr/bin/Xvfb :99 -screen 0 1600x1200x24 -auth /etc/conf.d/Xvfb.cfg -listen tcp
tomcat    7863     1 59 14:38 ?        00:00:51   /etc/java-config-2/current-system-vm/bin/java -Djava.util.logging.config.file=/opt/tomcat8/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderL
tomcat    7923  7863  0 14:38 ?        00:00:00     /opt/geckodriver --port=31399 -b /usr/bin/firefox-bin
tomcat    7943  7923  6 14:38 ?        00:00:04       /opt/firefox/firefox -marionette --headless -profile /tmp/rust_mozprofile.qpQfCj8DR1ab
tomcat    8032  7943  0 14:38 ?        00:00:00         /opt/firefox/plugin-container -greomni /opt/firefox/omni.ja -appomni /opt/firefox/browser/omni.ja -appdir /opt/firefox/browser 7943 true tab
tomcat    7977     1  0 14:38 ?        00:00:00   dbus-launch --autolaunch 32b951aa6b930bec1f195b765af588f5 --binary-syntax --close-stderr
tomcat    7978     1  0 14:38 ?        00:00:00   /usr/bin/dbus-daemon --fork --print-pid 5 --print-address 7 --session
tomcat    7993     1  0 14:38 ?        00:00:00   /usr/libexec/gconfd-2

创建 Firefox 实例的代码(使用更详细的日志级别来获取有关问题的更多信息):

    System.setProperty("webdriver.gecko.driver", driverGeckoPath);

    LoggingPreferences loggingPrefs = new LoggingPreferences();
    loggingPrefs.enable(LogType.BROWSER, Level.ALL);
    loggingPrefs.enable(LogType.CLIENT, Level.ALL);
    loggingPrefs.enable(LogType.DRIVER, Level.ALL);
    loggingPrefs.enable(LogType.PERFORMANCE, Level.ALL);
    loggingPrefs.enable(LogType.PROFILER, Level.ALL);
    loggingPrefs.enable(LogType.SERVER, Level.ALL);

    DesiredCapabilities desiredCapabilities = new DesiredCapabilities();
    desiredCapabilities.setCapability("marionette", true);
    desiredCapabilities.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
    desiredCapabilities.setCapability(CapabilityType.LOGGING_PREFS, loggingPrefs);

    FirefoxOptions options = new FirefoxOptions(desiredCapabilities);
    options.setLogLevel(FirefoxDriverLogLevel.TRACE);

    driver = new FirefoxDriver(options);

geckodriver 位于 /opt/ 并且是可执行的。

我当前的设置/配置


所有组件的版本均为 3.12.0(selenium-java、selenium-remote-driver、selenium-firefox-driver、selenium-api)

火狐
版本 60.0

壁虎司机
20.1版

Xvfb 配置

XVFB=/usr/bin/Xvfb
XVFBARGS=":99 -screen 0 1600x1200x24 -auth /etc/conf.d/Xvfb.cfg -nolisten tcp"
PIDFILE=/var/run/Xvfb.pid

Xvfb 配置

localhost

Xvfb init.d-script

start() {
        ebegin "Starting virtual X frame buffer: Xvfb"
        start-stop-daemon --start --pidfile $PIDFILE --make-pidfile --background --exec $XVFB -- $XVFBARGS
        eend ${?}
}

stop() {
        ebegin  "Stopping virtual X frame buffer: Xvfb"
        start-stop-daemon --stop --pidfile $PIDFILE
        eend ${?}
}

显示变量的导出(在启动时为所有用户初始化):

DISPLAY=:99

/etc/conf.d/主机名

gentoo

/etc/hosts

127.0.0.1       gentoo
127.0.0.1       localhost
::1             localhost


非常感谢任何提示/提示以获取有关如何调试/解决此问题的更多信息...


更新 1

我尝试使用 ChromeDriver 运行我的 selenium 组件,这很有趣。代码:

  System.setProperty("webdriver.chrome.driver", "/opt/chromedriver");
  driver = new ChromeDriver();

日志显示:

Starting ChromeDriver 2.38.552522 (437e6fbedfa8762dec75e2c5b3ddb86763dc9dcb) on port 24646
Only local connections are allowed.


更新 2

当我在本地环境(类似配置/gentoo)中运行应用程序时,一切正常。但我在这里没有使用 Xvfb,而是使用 xorg-server。 这是日志输出:

1526217622830   geckodriver INFO    geckodriver 0.20.1
1526217622833   geckodriver INFO    Listening on 127.0.0.1:12500
1526217622955   mozrunner::runner   INFO    Running command: "/usr/bin/firefox-bin" "-marionette" "-profile" "/tmp/rust_mozprofile.AZMw0iETRqFj"
1526217622955   geckodriver::marionette DEBUG   Waiting 60s to connect to browser
1526217623246   Marionette  DEBUG   Received observer notification profile-after-change
1526217623372   Marionette  DEBUG   Received observer notification command-line-startup
1526217623372   Marionette  DEBUG   Received observer notification nsPref:changed
1526217624744   Marionette  DEBUG   Received observer notification sessionstore-windows-restored
1526217624816   Marionette  DEBUG   Setting recommended pref toolkit.cosmeticAnimations.enabled to false
1526217624816   Marionette  DEBUG   Setting recommended pref datareporting.policy.dataSubmissionPolicyAccepted to false
1526217624816   Marionette  DEBUG   Setting recommended pref extensions.shield-recipe-client.api_url to 
1526217624847   Marionette  DEBUG   New connections are accepted
1526217624848   Marionette  INFO    Listening on port 34253
1526217624860   geckodriver::marionette DEBUG   Connected to Marionette on localhost:34253
1526217624868   Marionette  DEBUG   Accepted connection 0 from 127.0.0.1:56070

对我来说,这似乎是 Marionette 和 geckodriver 之间的连接问题。会不会是端口问题?在服务器上,Marionette 似乎总是在端口 2828 上监听,在我的本地机器上,它似乎总是一个随机端口......

【问题讨论】:

    标签: java selenium firefox marionette geckodriver


    【解决方案1】:

    对于那些可能担心的人,我遇到了同样的问题并陷入了“连接被拒绝”的嘉年华。经过整整两个晚上的尝试和错误,我发现我之前的 geckodriver、selenium 和 firefox 进程没有关闭,我有大约 50 个打开的进程。这三个简单的命令解决了我的问题

    pkill geckodriver
    pkill firefox
    pkill selenium
    

    【讨论】:

      【解决方案2】:

      此错误消息...

      1526203737802   geckodriver::marionette DEBUG   Waiting 60s to connect to browser
      1526203739872   Marionette      INFO    Listening on port 2828
      1526203797867   webdriver::server       DEBUG   <- 500 Internal Server Error {"value":{"error":"unknown error","message":"connection refused","stacktrace":""}}
      11:29:57.944 [pool-2-thread-1] ERROR test.test.test.test.test.Test - Exception occured while processing.
      org.openqa.selenium.WebDriverException: connection refused
      

      ...暗示 GeckoDriver 无法成功启动/生成新的 Web 客户端会话/实例。

      您的主要问题是您将DesiredCapabilities 类型的对象(即desiredCapabilities)分配给FirefoxOptions选项 的方式。正确的方法是将DesiredCapabilities 类对象,即desiredCapabilities 通过merge() 函数从MutableCapabilities 类传递到FirefoxOptions 类对象,如下所示: p>

      System.setProperty("webdriver.gecko.driver", driverGeckoPath);
      
      LoggingPreferences loggingPrefs = new LoggingPreferences();
      loggingPrefs.enable(LogType.BROWSER, Level.ALL);
      loggingPrefs.enable(LogType.CLIENT, Level.ALL);
      loggingPrefs.enable(LogType.DRIVER, Level.ALL);
      loggingPrefs.enable(LogType.PERFORMANCE, Level.ALL);
      loggingPrefs.enable(LogType.PROFILER, Level.ALL);
      loggingPrefs.enable(LogType.SERVER, Level.ALL);
      
      DesiredCapabilities desiredCapabilities = new DesiredCapabilities();
      desiredCapabilities.setCapability("marionette", true);
      desiredCapabilities.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
      desiredCapabilities.setCapability(CapabilityType.LOGGING_PREFS, loggingPrefs);
      
      FirefoxOptions options = new FirefoxOptions();
      options.merge(desiredCapabilities);
      options.setLogLevel(FirefoxDriverLogLevel.TRACE);
      
      driver = new FirefoxDriver(options);
      

      更新

      由于您仍然面临同样的错误,请考虑执行以下步骤:

      • 清理你的项目工作区通过你的IDE重建你的项目只需要依赖。
      • 使用CCleaner 工具在执行测试套件之前和之后清除所有操作系统杂务。
      • 如果您的基础 Web Client 版本太旧,请通过Revo Uninstaller 卸载它并安装最新的 GA 和发布版本的 Web Client
      • 进行系统重启
      • 以非 root 用户身份执行 Test
      • 始终在 tearDown(){} 方法中调用 driver.quit() 以优雅地关闭和销毁 WebDriverWeb Client 实例。

      【讨论】:

      • 正在使用更改后的代码重新部署应用程序,但仍然收到相同的错误消息。我还尝试将 Xvfb args 从 XVFBARGS=":99 -screen 0 1600x1200x24 -auth /etc/conf.d/Xvfb.cfg -nolisten tcp" 更改为 "XVFBARGS=":99" 但它也没有改变任何东西. 你还有什么想法我可以尝试吗?这可能是主机映射问题吗?当我在我的本地机器上运行 X 服务器时,我没有其他想法我可以在我的服务器上尝试什么...... :- (
      • @StephanM 查看我更新的答案,让我知道状态。
      • 与此同时,我正在尝试使用 ChromeDriver 运行我的 selenium 组件:System.setProperty("webdriver.chrome.driver", "/opt/chromedriver"); driver = new ChromeDriver();,它在日志中显示Starting ChromeDriver 2.38.552522 (437e6fbedfa8762dec75e2c5b3ddb86763dc9dcb) on port 24646 Only local connections are allowed. 并且正在运行。您建议的大多数步骤我都无法尝试使用 gentoo。正如我在标题中所写,它是一个托管在 tomcat 中的 web 应用程序。它在我的本地机器上使用 maven 构建,然后转移到我的服务器。
      • DebanjanB:你还有什么我可以尝试的想法吗?用新的结果/分析更新了我的帖子两次...
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-25
      • 1970-01-01
      • 2018-11-16
      • 1970-01-01
      • 1970-01-01
      • 2018-03-18
      相关资源
      最近更新 更多