0

我试图在 Chrome 中实现相同的逻辑并得到一个错误。如果有人可以帮助我解决问题,那就太好了。

代码编写:

require 'watir-webdriver'

class Selenium::WebDriver::Chrome::Service old_initialize = instance_method(:initialize)      
    define_method(:initialize) do |executable_path, port, *extra_args|
            old_initialize.bind(self).call(executable_path, port, '--silent', *extra_args)
    end
end

#Starts without diagnostic output
b = Watir::Browser.new :chrome

得到的错误信息:

ArgumentError: wrong number of arguments (3 for 2)
 from C:/Ruby193/lib/ruby/gems/1.9.1/gems/selenium-webdriver-2.29.0/lib/selenium/webdriver/chrome/service.rb:36:in initialize
 from (irb):5:in call
 from (irb):5:in block in
 from C:/Ruby193/lib/ruby/gems/1.9.1/gems/selenium-webdriver-2.29.0/lib/selenium/webdriver/chrome/service.rb:33:in new
 from C:/Ruby193/lib/ruby/gems/1.9.1/gems/selenium-webdriver-2.29.0/lib/selenium/webdriver/chrome/service.rb:33:in default_service
 from C:/Ruby193/lib/ruby/gems/1.9.1/gems/selenium-webdriver-2.29.0/lib/selenium/webdriver/chrome/bridge.rb:14:in initialize
 from C:/Ruby193/lib/ruby/gems/1.9.1/gems/selenium-webdriver-2.29.0/lib/selenium/webdriver/common/driver.rb:37:in new
 from C:/Ruby193/lib/ruby/gems/1.9.1/gems/selenium-webdriver-2.29.0/lib/selenium/webdriver/common/driver.rb:37:in for

我为此尝试了替代方案,但没有奏效。代码:

class Selenium::WebDriver::Chrome::Bridge
    old_extract_service_args = instance_method(:extract_service_args)
    define_method(:extract_service_args) do |opts|
        args = old_extract_service_args.bind(self).call(opts)
        if opts.has_key?(:service_silent) 
            opts.delete(:service_silent) 
            args << "--silent"
        end
        args
    end
end

#Starts without diagnostic output
b = Watir::Browser.new :chrome, :service_silent => true

#Starts with diagnostic output
b = Watir::Browser.new :chrome

获得的错误:

undefined method `extract_service_args' for class `Selenium::WebDriver::Chrome::Bridge' (NameError)
4

1 回答 1

1

您尝试执行的猴子补丁是为 selenium-webdriver v2.33.0(gem 的最新版本)编写的。根据异常,您使用的是 v2.29.0,它没有extra_args参数。

最好的解决方案是升级到你的 selenium-webdriver gem。

gem install selenium-webdrver

如果你必须使用 2.29.0 版本(带有 chromedriver v2.0),那么你将不得不做一个不同的猴子补丁。

require 'watir-webdriver'

module Selenium
    module WebDriver
        module Chrome
            class Service
                def initialize(executable_path, port)
                    @uri           = URI.parse "http://#{Platform.localhost}:#{port}"
                    server_command = [executable_path, "--port=#{port}", "--silent"]

                    @process       = ChildProcess.build(*server_command)
                    @socket_poller = SocketPoller.new Platform.localhost, port, START_TIMEOUT

                    @process.io.inherit! if $DEBUG == true
                end
            end
        end
    end
end

browser = Watir::Browser.new :chrome

浏览器将在没有初始诊断消息的情况下启动。请注意,这并没有解决 chromedriver 日志信息,这是不同的(最好通过升级到版本 2.33.0 再次处理)。

于 2013-07-02T16:10:01.570 回答