Cromedriver `driver.manage.logs.get(:browser)` fails on chromedriver 75.0.3770.8

Viewed 2922
6 Answers

Chrome 75 defaults to W3C mode, which doesn't specify a way to get log access.

The short term fix for this issue is to disable w3c via chromeOptions.

Capybara.register_driver :headless_chrome do |app|
  capabilities = Selenium::WebDriver::Remote::Capabilities.chrome(
    chromeOptions: { args: %w[headless window-size=1280,800], w3c: false },
  )

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

Capybara 3.24 now works around this issue when used with chromedriver >= 75.0.3770.90

As specified in the release notes for Chrome Driver 75, capability loggingPrefs has been renamed to goog:loggingPrefs, as required by W3C standard. Thus, the code setting the capabilities should be adjusted and there will be no necessity of falling back to non-w3c mode at least due to the log capturing reason.

As a short term fix, it seems you can monkey-patch the functionality back in (tested using Selenium-WebDriver v3.142.3):

You will need to add/patch the method to the Chrome::Bridge:

require 'selenium-webdriver'

module Selenium
  module WebDriver
    module Chrome
      module Bridge
        COMMANDS = remove_const(:COMMANDS).dup
        COMMANDS[:get_log] = [:post, 'session/:session_id/log']
        COMMANDS.freeze

        def log(type)
          data = execute :get_log, {}, {type: type.to_s}

          Array(data).map do |l|
            begin
              LogEntry.new l.fetch('level', 'UNKNOWN'), l.fetch('timestamp'), l.fetch('message')
            rescue KeyError
              next
            end
          end
        end
      end
    end
  end
end

In your capabilities, you will need to switch to using "goog:loggingPrefs" instead of just "loggingPrefs":

caps = Selenium::WebDriver::Remote::Capabilities.chrome('goog:loggingPrefs' => {browser: 'ALL'})
driver = Selenium::WebDriver.for(:chrome, desired_capabilities: caps)

driver.execute_script('console.log("test");')

puts driver.manage.logs.get(:browser)
#=> INFO 2019-06-13 21:48:03 -0400: console-api 362:34 "test"

This works for me:

Capybara.register_driver :chrome do |app|

  Capybara::Selenium::Driver.new(app, :browser => :chrome,   desired_capabilities: {
      "chromeOptions" => {
        w3c: false
      },
      'goog:loggingPrefs' => {browser: 'ALL'}
    })  
end

and in the log place

puts ""
puts "*** Browser logs:"
puts ""

puts page.driver.browser.manage.logs.get("browser").map { |log_entry|
  "[#{Time.at(log_entry.timestamp.to_i)}] [#{log_entry.level}] #{log_entry.message}"
}.join("\n")
Related