无法加载资源:net::ERR_UNKNOWN_URL_SCHEME 渲染带有水豚和无头的 jstree 时 chrome

Failed to load resource: net::ERR_UNKNOWN_URL_SCHEME when rendering jstree with with capybara and headless chrome

最近我在 rspec 测试中切换到无头 chrome。移动之后,我所有与 jstree 元素交互的测试都开始失败。下面是允许重现错误的最小代码:

Gemfile 中存在依赖关系:

# Gemfile
gem 'capybara', '2.18.0'
gem 'selenium-webdriver', '3.141.0'

这是呈现jstree的简单页面:

# jstree.html

<!doctype html>
<html>
  <head>
    <script src="https://rawgit.com/jquery/jquery/1.12.4/dist/jquery.min.js"></script>
    <script src="https://rawgit.com/vakata/jstree/3.3.7/dist/jstree.min.js"></script>
    <link href="https://rawgit.com/vakata/jstree/3.3.7/dist/themes/default/style.min.css" rel="stylesheet"/>
  </head>

  <body>
    <h1>This is a list:</h1>
    <ul id="tree"></ul>
    <script>
      $('#tree').jstree({
        core: {
          data: [{ "id": 1, "state": {}, "text": "Example", "parent": "#" }]
        }
      });
    </script>
  </body>
</html>

有一个ruby脚本可以用Capybara打开页面并保存页面截图:

# script.rb
require 'capybara'

require 'selenium-webdriver'
Capybara.register_driver :chrome do |app|
  options = ::Selenium::WebDriver::Chrome::Options.new(
    args: %w[headless no-sandbox disable-dev-shm-usage window-size=1280,800])

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

Capybara.default_driver = :chrome
Capybara.app = Rack::File.new File.expand_path('../', __FILE__)

include Capybara::DSL
visit '/jstree.html'
page.save_and_open_screenshot

当我 运行 bundle exec ruby script.rb 然后打开屏幕截图时,我看到以下内容:

但是当我在 chrome 浏览器中打开 jstree.html 时,我看到的是普通树:

当我将输出浏览器控制台消息添加到脚本末尾时

puts page.driver.browser.manage.logs.get(type)

然后重新运行 bundle exec ruby script.rb 我得到

SEVERE 2018-12-27 16:24:23 +0300: blob:http://127.0.0.1:49346/26695642-8b26-494b-a142-54a515db6512 - Failed to load resource: net::ERR_UNKNOWN_URL_SCHEME

我正在使用 chrome v71 和 chrome 驱动程序 v2.45

有人知道那里发生了什么吗? 我该如何解决这个问题?

不幸的是,Chrome 71 个 blob URL 在无头模式下损坏。如果你 运行 反对 Chrome 72 beta,它应该被修复。您可以在 puppeteer 问题中看到讨论 - https://github.com/GoogleChrome/puppeteer/issues/3463

另请注意,您不需要使用水豚截图 gem 来截取该屏幕截图 - 只需调用 page.save_and_open_screenshot