我试图在 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)