2013-04-30 62 views
0

我正在用ruby写这个bot,遇到了麻烦,这显示出来比我想象的要复杂得多。我读了很多类似的文章,但没有什么帮助。Watir不兼容的字符编码

它可能与watir无关,但只是为了具体我说的是。另外,我在Fedora上。这是我的尝试:

table = browser.table(:index , 7) 
puts table[1][1].text 

HTML表格片段是这样的:

<table> 
<tr> 
    something... 
</tr> 
<tr> 
    <td/> 
    <td> 
    Likiškėlių 
    </td> 
</tr> 
</table> 

我得到

/usr/local/share/gems/gems/multi_json-1.7.2/lib/multi_json/vendor/okjson.rb:364:in `[]=': incompatible character encodings: UTF-8 and ASCII-8BIT (Encoding::CompatibilityError) 
    from /usr/local/share/gems/gems/multi_json-1.7.2/lib/multi_json/vendor/okjson.rb:364:in `unquote' 
    from /usr/local/share/gems/gems/multi_json-1.7.2/lib/multi_json/vendor/okjson.rb:288:in `strtok' 
    from /usr/local/share/gems/gems/multi_json-1.7.2/lib/multi_json/vendor/okjson.rb:254:in `tok' 
    from /usr/local/share/gems/gems/multi_json-1.7.2/lib/multi_json/vendor/okjson.rb:217:in `lex' 
    from /usr/local/share/gems/gems/multi_json-1.7.2/lib/multi_json/vendor/okjson.rb:45:in `decode' 
    from /usr/local/share/gems/gems/multi_json-1.7.2/lib/multi_json/adapters/ok_json.rb:13:in `load' 
    from /usr/local/share/gems/gems/multi_json-1.7.2/lib/multi_json/adapter.rb:19:in `load' 
    from /usr/local/share/gems/gems/multi_json-1.7.2/lib/multi_json.rb:120:in `load' 
    from /usr/local/share/gems/gems/selenium-webdriver-2.32.1/lib/selenium/webdriver/common/json_helper.rb:11:in `json_load' 
    from /usr/local/share/gems/gems/selenium-webdriver-2.32.1/lib/selenium/webdriver/remote/http/common.rb:59:in `create_response' 
    from /usr/local/share/gems/gems/selenium-webdriver-2.32.1/lib/selenium/webdriver/remote/http/default.rb:66:in `request' 
    from /usr/local/share/gems/gems/selenium-webdriver-2.32.1/lib/selenium/webdriver/remote/http/common.rb:40:in `call' 
    from /usr/local/share/gems/gems/selenium-webdriver-2.32.1/lib/selenium/webdriver/remote/bridge.rb:619:in `raw_execute' 
    from /usr/local/share/gems/gems/selenium-webdriver-2.32.1/lib/selenium/webdriver/remote/bridge.rb:597:in `execute' 
    from /usr/local/share/gems/gems/selenium-webdriver-2.32.1/lib/selenium/webdriver/remote/bridge.rb:508:in `getElementText' 
    from /usr/local/share/gems/gems/selenium-webdriver-2.32.1/lib/selenium/webdriver/common/element.rb:108:in `text' 
    from /usr/local/share/gems/gems/watir-webdriver-0.6.4/lib/watir-webdriver/elements/element.rb:86:in `text' 
    from watir.rb:25:in `<main>' 

我也尝试:

.gsub!(/\P{ASCII}/, '') 
.delete!("^\u{0000}-\u{007F}") 

变化,但它没有解决。请帮忙!为什么编码如此复杂?我无能为力。

编辑:实际上,如果有帮助,我不需要打印字符串,但只写入文件,但错误是相同的。

和我的红宝石版本是:红宝石1.9.3p392(2013年2月22日修订39386)[i386的Linux的]

编辑:我也试过:

# encoding: ASCII-8BIT 
or 
# encoding: utf-8 
with 
puts table[1][1].text.length 

仍然在同样的错误

编辑因为它甚至不会显示.bytesize,我敢打赌它是watir。有什么建议么?

回答

相关问题