我正在用红宝石编写这个机器人并遇到麻烦,这比我想象的要复杂得多。我读了许多类似的文章,但没有什么真正帮助。
它可能与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。有什么建议吗?
答案 0 :(得分:0)
好的,我得到了一些东西。 I changed my watir browser from firefox to chrome.现在我得到编码的字符串,没有错误。中提琴!
答案 1 :(得分:0)
安装“json”或“oj”宝石将解决此问题。
有关支持的JSON引擎的完整列表,请参阅https://github.com/intridea/multi_json。