0

我正在用 ruby​​ 编写这个机器人并遇到了麻烦,这比我想象的要复杂得多。我阅读了许多类似的文章,但没有任何帮助。

它可能与 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}")

变化,但没有成功。请帮忙!为什么编码这么复杂?我一无所知。

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

我的 ruby​​ 版本是:ruby 1.9.3p392(2013-02-22 修订版 39386)[i386-linux]

编辑:我也试过:

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

但仍然是同样的错误

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

4

2 回答 2

0

好的,我得到了一些东西。我将我的 watir 浏览器从 firefox 更改为 chrome。现在我得到编码的字符串并且没有错误。中提琴!

于 2013-05-01T00:00:31.460 回答
0

安装“json”或“oj”gem 将解决这个问题。

有关支持的 JSON 引擎的完整列表,请参阅https://github.com/intridea/multi_json

于 2014-02-18T01:57:24.917 回答