无法使用watir-webdriver创建firefox浏览器

时间:2015-04-04 00:04:49

标签: ruby watir watir-webdriver

我有使用我的watir-webdriver版本的firefox 20。我使用以下代码可以打开任何浏览器或无头phantomjs浏览器。 Chrome和幻像工作,但firefox抛出异常。

为什么呢?我该如何解决这个问题?

require 'rubygems'
require 'watir-webdriver'
require 'phantomjs'

def get_browser(browser_name)
  browser = nil
  if browser_name != nil    
    puts "\n#{browser_name} created\n"  
    browser = Watir::Browser.new browser_name.to_sym
  else
    puts "creating phantom browser !"
    browser = Watir::Browser.new :phantomjs
    puts "\nphantom created\n"
  end
  return browser
end

browser_name = ARGV[0]
browser = get_browser(browser_name)
puts "\n\n"

browser.close

异常 -

C:/code/ruby/lib/ruby/gems/1.8/gems/json_pure-1.4.3/lib/json/pure/generator.rb:393:in `to_json': undefined method `ascii_only?' for {}:Hash (NoMethodError)
        from C:/code/ruby/lib/ruby/gems/1.8/gems/multi_json-1.3.7/lib/multi_json/adapters/json_common.rb:11:in `dump'
        from C:/code/ruby/lib/ruby/gems/1.8/gems/multi_json-1.3.7/lib/multi_json.rb:115:in `dump'
        from C:/code/ruby/lib/ruby/gems/1.8/gems/selenium-webdriver-2.33.0/lib/selenium/webdriver/common/json_helper.rb:23:in `json_dump'
        from C:/code/ruby/lib/ruby/gems/1.8/gems/selenium-webdriver-2.33.0/lib/selenium/webdriver/firefox/profile.rb:254:in `write_prefs'
        from C:/code/ruby/lib/ruby/gems/1.8/gems/selenium-webdriver-2.33.0/lib/selenium/webdriver/firefox/profile.rb:253:in `each'
        from C:/code/ruby/lib/ruby/gems/1.8/gems/selenium-webdriver-2.33.0/lib/selenium/webdriver/firefox/profile.rb:253:in `write_prefs'
        from C:/code/ruby/lib/ruby/gems/1.8/gems/selenium-webdriver-2.33.0/lib/selenium/webdriver/firefox/profile.rb:252:in `open'
        from C:/code/ruby/lib/ruby/gems/1.8/gems/selenium-webdriver-2.33.0/lib/selenium/webdriver/firefox/profile.rb:252:in `write_prefs'
        from C:/code/ruby/lib/ruby/gems/1.8/gems/selenium-webdriver-2.33.0/lib/selenium/webdriver/firefox/profile.rb:232:in `update_user_prefs_in'
        from C:/code/ruby/lib/ruby/gems/1.8/gems/selenium-webdriver-2.33.0/lib/selenium/webdriver/firefox/profile.rb:79:in `layout_on_disk'
        from C:/code/ruby/lib/ruby/gems/1.8/gems/selenium-webdriver-2.33.0/lib/selenium/webdriver/firefox/launcher.rb:58:in `create_profile'
        from C:/code/ruby/lib/ruby/gems/1.8/gems/selenium-webdriver-2.33.0/lib/selenium/webdriver/firefox/launcher.rb:34:in `launch'
        from C:/code/ruby/lib/ruby/gems/1.8/gems/selenium-webdriver-2.33.0/lib/selenium/webdriver/firefox/socket_lock.rb:20:in `locked'
        from C:/code/ruby/lib/ruby/gems/1.8/gems/selenium-webdriver-2.33.0/lib/selenium/webdriver/firefox/launcher.rb:32:in `launch'
        from C:/code/ruby/lib/ruby/gems/1.8/gems/selenium-webdriver-2.33.0/lib/selenium/webdriver/firefox/bridge.rb:24:in `initialize'
        from C:/code/ruby/lib/ruby/gems/1.8/gems/selenium-webdriver-2.33.0/lib/selenium/webdriver/common/driver.rb:31:in `new'
        from C:/code/ruby/lib/ruby/gems/1.8/gems/selenium-webdriver-2.33.0/lib/selenium/webdriver/common/driver.rb:31:in `for'
        from C:/code/ruby/lib/ruby/gems/1.8/gems/selenium-webdriver-2.33.0/lib/selenium/webdriver.rb:67:in `for'
        from C:/code/ruby/lib/ruby/gems/1.8/gems/watir-webdriver-0.6.2/lib/watir-webdriver/browser.rb:46:in `initialize'
        from watir-browser.rb:9:in `new'
        from watir-browser.rb:9:in `get_browser'
        from watir-browser.rb:19

2 个答案:

答案 0 :(得分:1)

你的代码对我有用而没有问题。

$ ruby test.rb
creating phantom browser !
phantom created

$ ruby test.rb firefox
firefox created

$ ruby test.rb chrome
chrome created

$ gem list | grep webdriver
selenium-webdriver (2.45.0, 2.44.0)
watir-webdriver (0.7.0, 0.6.11)

Firefox附带selenium-webdriver,您可能只需要更新您的gem(s)

答案 1 :(得分:0)

Ruby 1.8,Firefox 20,Watir 0.6.2都看起来已经过时了2年。 json_pure-1.4.3但是5岁 - https://rubygems.org/gems/json_pure/versions

尝试更新那个。