在WSL(Ubuntu 18.04)中,Chrome无法在Chromedriver下通过“ DevToolsActivePort文件不存在”启动

时间:2019-07-15 22:39:50

标签: ruby-on-rails google-chrome selenium-chromedriver ubuntu-18.04 windows-subsystem-for-linux

我看到错误

Selenium::WebDriver::Error::UnknownError: unknown error: Chrome failed to start: crashed
  (unknown error: DevToolsActivePort file doesn't exist)
  (The process started from chrome location /usr/bin/google-chrome is no longer running, so ChromeDriver is assuming that Chrome has crashed.)
#0 0x7f87e934a7a9 <unknown>

当尝试在WSL(适用于Linux的Windows子系统)下,通过Capybara在Rails(5.2.3)中通过Selenium :: Chromedriver启动无头的Chrome时,该操作系统使用Ubuntu选项:headless,{{1 }}和disable-gpu(除了尝试使用选项single-processno-sandboxproxy-server="direct://"disable-dev-shm-usage之外,其他所有选项均无效)。

proxy-bypass-list="*"

Gemfile.lock

我尝试了多种其他方法来实现此目的,包括允许Chromedriver尝试发现正在使用的Chrome版本(由于可能的相关原因而失败,但我不能完全确定),使用符号链接指向Chrome的Windows可执行文件(在这种情况下,它不接受必要的标志,也不会启动,我想这并不奇怪)。

如果我在命令行中使用这些标志启动它,则会得到与 GEM remote: https://rubygems.org/ specs: actioncable (5.2.3) actionpack (= 5.2.3) nio4r (~> 2.0) websocket-driver (>= 0.6.1) actionmailer (5.2.3) actionpack (= 5.2.3) actionview (= 5.2.3) activejob (= 5.2.3) mail (~> 2.5, >= 2.5.4) rails-dom-testing (~> 2.0) actionpack (5.2.3) actionview (= 5.2.3) activesupport (= 5.2.3) rack (~> 2.0) rack-test (>= 0.6.3) rails-dom-testing (~> 2.0) rails-html-sanitizer (~> 1.0, >= 1.0.2) actionview (5.2.3) activesupport (= 5.2.3) builder (~> 3.1) erubi (~> 1.4) rails-dom-testing (~> 2.0) rails-html-sanitizer (~> 1.0, >= 1.0.3) activejob (5.2.3) activesupport (= 5.2.3) globalid (>= 0.3.6) activemodel (5.2.3) activesupport (= 5.2.3) activerecord (5.2.3) activemodel (= 5.2.3) activesupport (= 5.2.3) arel (>= 9.0) activestorage (5.2.3) actionpack (= 5.2.3) activerecord (= 5.2.3) marcel (~> 0.3.1) activesupport (5.2.3) concurrent-ruby (~> 1.0, >= 1.0.2) i18n (>= 0.7, < 2) minitest (~> 5.1) tzinfo (~> 1.1) addressable (2.6.0) public_suffix (>= 2.0.2, < 4.0) arel (9.0.0) backports (3.12.0) bootsnap (1.4.4) msgpack (~> 1.0) builder (3.2.3) byebug (11.0.1) capybara (3.14.0) addressable mini_mime (>= 0.1.3) nokogiri (~> 1.8) rack (>= 1.6.0) rack-test (>= 0.6.3) regexp_parser (~> 1.2) xpath (~> 3.2) childprocess (1.0.1) rake (< 13.0) coffee-rails (5.0.0) coffee-script (>= 2.2.0) railties (>= 5.2.0) coffee-script (2.4.1) coffee-script-source execjs coffee-script-source (1.12.2) concurrent-ruby (1.1.5) crass (1.0.4) cucumber (3.1.2) builder (>= 2.1.2) cucumber-core (~> 3.2.0) cucumber-expressions (~> 6.0.1) cucumber-wire (~> 0.0.1) diff-lcs (~> 1.3) gherkin (~> 5.1.0) multi_json (>= 1.7.5, < 2.0) multi_test (>= 0.1.2) cucumber-core (3.2.1) backports (>= 3.8.0) cucumber-tag_expressions (~> 1.1.0) gherkin (~> 5.0) cucumber-expressions (6.0.1) cucumber-rails (1.6.0) capybara (>= 1.1.2, < 4) cucumber (>= 3.0.2, < 4) mime-types (>= 1.17, < 4) nokogiri (~> 1.8) railties (>= 4, < 6) cucumber-tag_expressions (1.1.1) cucumber-wire (0.0.1) diff-lcs (1.3) domain_name (0.5.20180417) unf (>= 0.0.5, < 1.0.0) erubi (1.8.0) execjs (2.7.0) ffi (1.11.1) gherkin (5.1.0) globalid (0.4.2) activesupport (>= 4.2.0) haml (5.1.1) temple (>= 0.8.0) tilt http-cookie (1.0.3) domain_name (~> 0.5) i18n (1.6.0) concurrent-ruby (~> 1.0) jquery-rails (4.3.3) rails-dom-testing (>= 1, < 3) railties (>= 4.2.0) thor (>= 0.14, < 2.0) listen (3.1.5) rb-fsevent (~> 0.9, >= 0.9.4) rb-inotify (~> 0.9, >= 0.9.7) ruby_dep (~> 1.2) loofah (2.2.3) crass (~> 1.0.2) nokogiri (>= 1.5.9) mail (2.7.1) mini_mime (>= 0.1.1) marcel (0.3.3) mimemagic (~> 0.3.2) method_source (0.9.2) mime-types (3.2.2) mime-types-data (~> 3.2015) mime-types-data (3.2019.0331) mimemagic (0.3.3) mini_mime (1.0.1) mini_portile2 (2.4.0) minitest (5.11.3) msgpack (1.3.0) multi_json (1.13.1) multi_test (0.1.2) netrc (0.11.0) nio4r (2.3.1) nokogiri (1.10.3) mini_portile2 (~> 2.4.0) public_suffix (3.0.3) puma (3.12.1) rack (2.0.7) rack-test (1.1.0) rack (>= 1.0, < 3) rails (5.2.3) actioncable (= 5.2.3) actionmailer (= 5.2.3) actionpack (= 5.2.3) actionview (= 5.2.3) activejob (= 5.2.3) activemodel (= 5.2.3) activerecord (= 5.2.3) activestorage (= 5.2.3) activesupport (= 5.2.3) bundler (>= 1.3.0) railties (= 5.2.3) sprockets-rails (>= 2.0.0) rails-dom-testing (2.0.3) activesupport (>= 4.2.0) nokogiri (>= 1.6) rails-html-sanitizer (1.0.4) loofah (~> 2.2, >= 2.2.2) railties (5.2.3) actionpack (= 5.2.3) activesupport (= 5.2.3) method_source rake (>= 0.8.7) thor (>= 0.19.0, < 2.0) rake (12.3.2) rb-fsevent (0.10.3) rb-inotify (0.10.0) ffi (~> 1.0) regexp_parser (1.3.0) rest-client (2.0.2) http-cookie (>= 1.0.2, < 2.0) mime-types (>= 1.16, < 4.0) netrc (~> 0.8) rspec-core (3.8.0) rspec-support (~> 3.8.0) rspec-expectations (3.8.2) diff-lcs (>= 1.2.0, < 2.0) rspec-support (~> 3.8.0) rspec-mocks (3.8.0) diff-lcs (>= 1.2.0, < 2.0) rspec-support (~> 3.8.0) rspec-rails (3.8.2) actionpack (>= 3.0) activesupport (>= 3.0) railties (>= 3.0) rspec-core (~> 3.8.0) rspec-expectations (~> 3.8.0) rspec-mocks (~> 3.8.0) rspec-support (~> 3.8.0) rspec-support (3.8.0) ruby_dep (1.5.0) rubyzip (1.2.3) sassc (2.0.1) ffi (~> 1.9) rake sassc-rails (2.1.0) railties (>= 4.0.0) sassc (>= 2.0) sprockets (> 3.0) sprockets-rails tilt selenium-webdriver (3.142.3) childprocess (>= 0.5, < 2.0) rubyzip (~> 1.2, >= 1.2.2) spring (2.1.0) spring-watcher-listen (2.0.1) listen (>= 2.7, < 4.0) spring (>= 1.2, < 3.0) sprockets (3.7.2) concurrent-ruby (~> 1.0) rack (> 1, < 3) sprockets-rails (3.2.1) actionpack (>= 4.0) activesupport (>= 4.0) sprockets (>= 3.0.0) temple (0.8.1) thor (0.20.3) thread_safe (0.3.6) tilt (2.0.9) turbolinks (5.2.0) turbolinks-source (~> 5.2) turbolinks-source (5.2.0) tzinfo (1.2.5) thread_safe (~> 0.1) uglifier (4.1.20) execjs (>= 0.3.0, < 3) unf (0.1.4) unf_ext unf_ext (0.0.7.6) webdrivers (4.1.0) nokogiri (~> 1.6) rubyzip (~> 1.0) selenium-webdriver (>= 3.0, < 4.0) websocket-driver (0.7.1) websocket-extensions (>= 0.1.0) websocket-extensions (0.1.4) xpath (3.2.0) nokogiri (~> 1.8) PLATFORMS ruby DEPENDENCIES bootsnap (>= 1.1.0) byebug coffee-rails cucumber-rails haml jquery-rails listen (>= 3.0.5, < 3.2) puma (~> 3.11) rails (~> 5.2.2) rest-client rspec-rails sassc-rails spring spring-watcher-listen (~> 2.0.0) turbolinks tzinfo-data uglifier webdrivers (~> 4.0) RUBY VERSION ruby 2.6.1p33 BUNDLED WITH 2.0.2 稍有不同的响应集,但是它们归结为google-chrome(带有The GPU process has crashed)和{{ 1}}(将--headless --no-sandbox --disable-gpu添加到该列表)。

由于我暂时停留在此开发环境中,因此很乐意接受任何建议或要求提供信息。

3 个答案:

答案 0 :(得分:3)

我遇到了同样的问题,几个小时后我发现了问题以及解决方法:

Google从Chrome 75以来更改了必须指定chromeOptions的方式。将chromeOptions更改为goog:chromeOptions

我将其更改为:

capabilities = Selenium::WebDriver::Remote::Capabilities.chrome(
    'goog:chromeOptions' => { args: %w[headless disable-gpu window-size=1920,1080 no-sandbox disable-dev-shm-usage] }
)

答案 1 :(得分:0)

我花了很多时间,但是关键是@Samuel的答案和@twistezo的修改。这是我完整的解决方案:

请注意,根据@ Samuel,chromeOptions已修改为'goog:chromeOptions'。我不必在Windows中安装任何东西,但确实在Linux分区中安装了chrome。

# spec/rails_helper.rb
Capybara.register_driver :headless_chrome do |app|
  capabilities = Selenium::WebDriver::Remote::Capabilities.chrome(
      'goog:chromeOptions': { args: %w(no-sandbox headless disable-gpu window-size=1280,1024 disable-features=VizDisplayCompositor) } )

  Capybara::Selenium::Driver.new(app, browser: :chrome, desired_capabilities: capabilities)
end

然后进行相关测试:

        describe 'the tests that need javascript', js: true do
          before(:all) do
            # Selenium::WebDriver.logger.level = :debug
            Capybara.current_driver = :headless_chrome
            Capybara.javascript_driver = :headless_chrome
          end

          after(:all) do
            Capybara.use_default_driver
          end

          it 'is reachable with javascript enabled' do
            visit root_path
            expect(page.current_path).to eq(root_path)
            assert page.evaluate_script('jQuery.active').zero?
          end
        end

更新到Google Chrome 79.0.3945.88

args: %w(headless window-size=1280,1024 no-sandbox enable-features=NetworkService,NetworkServiceInProcess  disable-features=VizDisplayCompositor)

答案 2 :(得分:0)

我发现最好的解决方案是不要从WSL无头运行,而是从Windows启动chrome。两种方式都做完之后,我强烈喜欢这个选项,因为:

  1. 测试更加一致。我在Ubuntu中运行无头Chrome 有来去往往的失败。这个问题消失了 在Windows中运行headfull。
  2. 如果使用chromedriver --verbose
  3. ,则可以查看详细的日志
  4. 如果您在测试期间暂停执行,则可以看到页面并进行调试。
  5. 我花了很长时间摆弄无头的镀铬选项,以获得我的 运行测试;然后再升级到Chrome版本79。 使用chromedriver,我不需要任何选择。
  6. 看到您的测试运行很酷!

为此:

  1. 下载Chromedriver for Windows。确保你得到 与您的Chrome版本匹配的版本。将其解压缩。我用c:\ dev
  2. 转到Windows命令提示符并启动chromedriver C:\dev\chromedriver> chromedriver。它会告诉您端口 使用。您将在下一步中需要它。我的显示 Starting ChromeDriver 79.0.3945.36 (...) on port 9515
  3. 在您的rails_helper.rb
Capybara.register_driver :windows_chrome do |app|
  capabilities = Selenium::WebDriver::Remote::Capabilities.chrome()
  puts 'Current driver (windows_chrome) requires chromedriver to be launched from windows (C:\dev\chromedriver>chromedriver)'
  Capybara::Selenium::Driver.new(app,browser: :chrome, url: 'http://localhost:9515',
                                     desired_capabilities: capabilities)
end

#Capybara.default_driver = :windows_chrome
Capybara.javascript_driver = :windows_chrome
Capybara.default_max_wait_time = 5 # Seconds
#Selenium::WebDriver.logger.level = :debug
# Webdrivers.logger.level = :DEBUG