连接被 phantomjs 拒绝了 Watir webdriver

Connection refuse Watir webdriver with phantomjs

Watir gem 有问题,线程中 运行。基本上它看起来像这样: 我正在创建线程(假设有三个线程)。然后我正在创建新的浏览器,访问网页并尝试关闭该浏览器。在 2-3 小时内一切正常,然后我收到此错误(browser.close 导致此错误):

Errno::ECONNREFUSED: Connection refused - Connection refused
       initialize at org/jruby/ext/socket/RubyTCPSocket.java:124
             open at org/jruby/RubyIO.java:1123
 block in connect at /home/ubuntu/.rvm/rubies/jruby-9.0.4.0/lib/ruby/stdlib/net/http.rb:883
          timeout at org/jruby/ext/timeout/Timeout.java:128
          connect at /home/ubuntu/.rvm/rubies/jruby-9.0.4.0/lib/ruby/stdlib/net/http.rb:882
         do_start at /home/ubuntu/.rvm/rubies/jruby-9.0.4.0/lib/ruby/stdlib/net/http.rb:867
            start at /home/ubuntu/.rvm/rubies/jruby-9.0.4.0/lib/ruby/stdlib/net/http.rb:856
            start at /home/ubuntu/.rvm/rubies/jruby-9.0.4.0/lib/ruby/stdlib/net/http.rb:583
             stop at /home/ubuntu/.rvm/gems/jruby-9.0.4.0/gems/selenium-webdriver-2.48.1/lib/selenium/webdriver/phantomjs/service.rb:75
             quit at /home/ubuntu/.rvm/gems/jruby-9.0.4.0/gems/selenium-webdriver-2.48.1/lib/selenium/webdriver/phantomjs/bridge.rb:73
             quit at /home/ubuntu/.rvm/gems/jruby-9.0.4.0/gems/selenium-webdriver-2.48.1/lib/selenium/webdriver/common/driver.rb:171
            close at /home/ubuntu/.rvm/gems/jruby-9.0.4.0/gems/watir-webdriver-0.9.1/lib/watir-webdriver/browser.rb:136

这是代码:

THREAD_COUNT.times.map {
  Thread.new(links) do |links|
    while link = mutex.synchronize { links.pop }
      counter = 0
      sleep(rand 1..50)
      browser = Watir::Browser.new :phantomjs, :args => %w(--ssl-protocol=tlsv1 --ignore-ssl-errors=yes)
      begin
       result = function(browser)
      rescue
       counter += 1
       retry if counter > 2
      end
      browser.close
    end
  end
}.each(&:join)

我是 运行 这个 ubuntu 和 jruby 9.0.4.0 正如这里所建议的,我添加了睡眠 Opening several threads with watir-webdriver results in 'Connection refused' error 但它不起作用

如您所见,我在 while 循环中创建了新的浏览器实例。有时它无法按时关闭该浏览器或发生其他与 pid 相关的故障。最好创建固定数量的浏览器(例如 THREAD_COUNT 次)并在可用时重用它。因此,在 THREAD_COUNT.times.map 之后立即创建浏览器将为每个线程提供一个浏览器实例。