我尝试在Chrome中实施same logic并收到错误消息。如果有人可以帮我解决问题,那就太好了。
代码编写:
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)
答案 0 :(得分:1)
您尝试执行的猴子补丁是为selenium-webdriver v2.33.0(宝石的最新版本)编写的。基于异常,您使用的是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来处理)。