当我对类型为"select-one"的元素调用to_sym时,我遇到了一个错误。
begin
["//select", "//input"].each do |t|
puts 'finding input for : ' + t
elements = all(:xpath, t)
elements.each do |e|
puts "found element #{e[:id]} of type #{e[:type]}" if @debug
puts "to symbol result: " + e[:id].to_sym #This line explodes
#...
rescue
puts "failed to enter fields on #{@name}"
raise
end
我得到错误"Failed to enter fields on pageName"。当我在类型为select-one的元素上调用to_sym
时,会发生此错误。我怎样才能查明这个错误的原因并解决它?
Per @axeltetzlaff我安装了撬。我注意到值[salutation]返回nil,我期望下面给出的值:
[1] pry(#<Step>)> values[e[:salutation]]
=> nil
[2] pry(#<Step>)> values
=> {:z=>"z",
:a=>"a",
:b=>"b",
:c=>"c",
:d=>"d",
:e=>"e",
:f=>"f",
:g=>"",
:h=>"",
:salutation=>"Mr.",#See...I have a value
更新2
我拿出了用于调试的puts
,问题消失了:
puts "to symbol result: " + e[:id].to_sym #This line explodes
代码在下一行不再中断。通过简单的推断,问题是我不能连接字符串和符号。我猜有一些ruby规则说我不能这样做,但我没有一个可用。
to_sym
将字符串转换为符号。例如,"a".to_sym
变成:a
确保你的e[:id]
返回一个你正在调用to_sym
方法的字符串对象。试着检查:
puts e[:id].inspect
puts e[:id].class
更新:
在Ruby中不能连接字符串和符号。这将抛出no implicit conversion of Symbol into String (TypeError)
异常
而不是:
puts "to symbol result: " + e[:id].to_sym
你也可以这样做:
puts "to symbol result: #{e[:id].to_sym}"