17

While accessing driver.manage.logs.get(:browser) on chromedriver 75.0.3770.8 I'm getting this error:

undefined method `log' for #<Selenium::WebDriver::Remote::W3C::Bridge:0x0000562dc661c250> (NoMethodError)

Works fine on 74.0.3729.6

from: https://github.com/SeleniumHQ/selenium/issues/7270

Gareth Jones
  • 1,241
  • 4
  • 17
  • 38
Daniel
  • 4,051
  • 2
  • 28
  • 46

7 Answers7

11

in recent selenium-webdriver (4.4.0) with recent Chrome (105), manage.logs is gone but this works:

page.driver.browser.logs.get(:browser)
jaynetics
  • 1,234
  • 12
  • 15
8

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
Daniel
  • 4,051
  • 2
  • 28
  • 46
6

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

Thomas Walpole
  • 48,548
  • 5
  • 64
  • 78
  • Thanks for the hint! Unfortunately, Capybara 3.16+ does not work with Ruby 2.3. – codener Jun 18 '19 at 06:27
  • 3
    @codener yeah Ruby 2.3 went end of life - time to upgrade – Thomas Walpole Jun 18 '19 at 07:42
  • @Cristik I disagree with that - everyone is able to upgrade but some people determine the time/money aren't worth it to them. If you're still running production code with Ruby 2.3 you've made the determination that your legal liability around security issues is outweighed by the costs to upgrade and that's your right, but if you've made that decision why would you still be wanting to run tests with modern test tool versions - it's not like you're going to actually spend the time/money to fix anything they find. – Thomas Walpole Mar 31 '21 at 15:46
4

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.

Alexey Subach
  • 11,903
  • 7
  • 34
  • 60
2

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"
Justin Ko
  • 46,526
  • 5
  • 91
  • 101
2

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")
Hussam Kurd
  • 8,066
  • 2
  • 43
  • 38
2

I've found that just upgrading the selenium-webdriver gem to 3.142.4+ fixes the issue (https://github.com/SeleniumHQ/selenium/blob/trunk/rb/CHANGES) even if you are using an outdated Capybara.

driver.browser.manage.logs.get(:browser) works just as before.