0

以下是我用来从选择标签中选择一个特定选项的代码:

       select = driver.find_element(:id, "lang")
       wait = Selenium::WebDriver::Wait.new(:timeout => 20)
       begin
         all_options = wait.until { driver.find_elements(:tag_name => "option") }
       end
       all_options.each do |option|
             if (option.attribute("value")=="11")
                 option.click
             end
       end

我正在使用 jruby。当我在 IRB 中逐行执行命令时,没有问题。但是,当我一起运行整个代码时,它给了我一个意外的文件结束错误。为什么我会收到此错误?为什么它在逐个命令执行时起作用,而不是在整体执行时起作用?我该如何克服呢?

错误如下 -

SyntaxError: (irb):364: syntax error, unexpected end-of-file

from org/jruby/RubyKernel.java:1061:in `eval'
from C:/jruby-1.7.1/lib/ruby/1.8/irb.rb:158:in `eval_input'
from C:/jruby-1.7.1/lib/ruby/1.8/irb.rb:271:in `signal_status'
from C:/jruby-1.7.1/lib/ruby/1.8/irb.rb:270:in `signal_status'
from C:/jruby-1.7.1/lib/ruby/1.8/irb.rb:155:in `eval_input'
from org/jruby/RubyKernel.java:1392:in `loop'
from org/jruby/RubyKernel.java:1160:in `catch'
from C:/jruby-1.7.1/lib/ruby/1.8/irb.rb:154:in `eval_input'
from C:/jruby-1.7.1/lib/ruby/1.8/irb.rb:71:in `start'
from org/jruby/RubyKernel.java:1160:in `catch'
from C:/jruby-1.7.1/lib/ruby/1.8/irb.rb:70:in `start'
from C:\jruby-1.7.1\/bin/jirb_swing:54:in `(root)'
SyntaxError: (irb):364: syntax error, unexpected kEND

end
^
from org/jruby/RubyKernel.java:1061:in `eval'
from C:/jruby-1.7.1/lib/ruby/1.8/irb.rb:158:in `eval_input'
from C:/jruby-1.7.1/lib/ruby/1.8/irb.rb:271:in `signal_status'
from C:/jruby-1.7.1/lib/ruby/1.8/irb.rb:270:in `signal_status'
from C:/jruby-1.7.1/lib/ruby/1.8/irb.rb:155:in `eval_input'
from org/jruby/RubyKernel.java:1392:in `loop'
from org/jruby/RubyKernel.java:1160:in `catch'
from C:/jruby-1.7.1/lib/ruby/1.8/irb.rb:154:in `eval_input'
from C:/jruby-1.7.1/lib/ruby/1.8/irb.rb:71:in `start'
from org/jruby/RubyKernel.java:1160:in `catch'
from C:/jruby-1.7.1/lib/ruby/1.8/irb.rb:70:in `start'
from C:\jruby-1.7.1\/bin/jirb_swing:54:in `(root)'
SyntaxError: (irb):367: syntax error, unexpected end-of-file

from org/jruby/RubyKernel.java:1061:in `eval'
from C:/jruby-1.7.1/lib/ruby/1.8/irb.rb:158:in `eval_input'
from C:/jruby-1.7.1/lib/ruby/1.8/irb.rb:271:in `signal_status'
from C:/jruby-1.7.1/lib/ruby/1.8/irb.rb:270:in `signal_status'
from C:/jruby-1.7.1/lib/ruby/1.8/irb.rb:155:in `eval_input'
from org/jruby/RubyKernel.java:1392:in `loop'
from org/jruby/RubyKernel.java:1160:in `catch'
from C:/jruby-1.7.1/lib/ruby/1.8/irb.rb:154:in `eval_input'
from C:/jruby-1.7.1/lib/ruby/1.8/irb.rb:71:in `start'
from org/jruby/RubyKernel.java:1160:in `catch'
from C:/jruby-1.7.1/lib/ruby/1.8/irb.rb:70:in `start'
from C:\jruby-1.7.1\/bin/jirb_swing:54:in `(root)'
NameError: undefined local variable or method `option' for main:Object
from (irb):367:in `evaluate'
from org/jruby/RubyKernel.java:1061:in `eval'
from C:/jruby-1.7.1/lib/ruby/1.8/irb.rb:158:in `eval_input'
from C:/jruby-1.7.1/lib/ruby/1.8/irb.rb:271:in `signal_status'
from C:/jruby-1.7.1/lib/ruby/1.8/irb.rb:270:in `signal_status'
from C:/jruby-1.7.1/lib/ruby/1.8/irb.rb:155:in `eval_input'
from org/jruby/RubyKernel.java:1392:in `loop'
from org/jruby/RubyKernel.java:1160:in `catch'
from C:/jruby-1.7.1/lib/ruby/1.8/irb.rb:154:in `eval_input'
from C:/jruby-1.7.1/lib/ruby/1.8/irb.rb:71:in `start'
from org/jruby/RubyKernel.java:1160:in `catch'
from C:/jruby-1.7.1/lib/ruby/1.8/irb.rb:70:in `start'
from C:\jruby-1.7.1\/bin/jirb_swing:54:in `(root)'
SyntaxError: (irb):368: syntax error, unexpected kEND

end
    ^
from org/jruby/RubyKernel.java:1061:in `eval'
from C:/jruby-1.7.1/lib/ruby/1.8/irb.rb:158:in `eval_input'
from C:/jruby-1.7.1/lib/ruby/1.8/irb.rb:271:in `signal_status'
from C:/jruby-1.7.1/lib/ruby/1.8/irb.rb:270:in `signal_status'
from C:/jruby-1.7.1/lib/ruby/1.8/irb.rb:155:in `eval_input'
from org/jruby/RubyKernel.java:1392:in `loop'
from org/jruby/RubyKernel.java:1160:in `catch'
from C:/jruby-1.7.1/lib/ruby/1.8/irb.rb:154:in `eval_input'
from C:/jruby-1.7.1/lib/ruby/1.8/irb.rb:71:in `start'
from org/jruby/RubyKernel.java:1160:in `catch'
from C:/jruby-1.7.1/lib/ruby/1.8/irb.rb:70:in `start'
from C:\jruby-1.7.1\/bin/jirb_swing:54:in `(root)'
SyntaxError: (irb):369: syntax error, unexpected kEND

 end
 ^
from org/jruby/RubyKernel.java:1061:in `eval'
from C:/jruby-1.7.1/lib/ruby/1.8/irb.rb:158:in `eval_input'
from C:/jruby-1.7.1/lib/ruby/1.8/irb.rb:271:in `signal_status'
from C:/jruby-1.7.1/lib/ruby/1.8/irb.rb:270:in `signal_status'
from C:/jruby-1.7.1/lib/ruby/1.8/irb.rb:155:in `eval_input'
from org/jruby/RubyKernel.java:1392:in `loop'
from org/jruby/RubyKernel.java:1160:in `catch'
from C:/jruby-1.7.1/lib/ruby/1.8/irb.rb:154:in `eval_input'
from C:/jruby-1.7.1/lib/ruby/1.8/irb.rb:71:in `start'
from org/jruby/RubyKernel.java:1160:in `catch'
from C:/jruby-1.7.1/lib/ruby/1.8/irb.rb:70:in `start'
from C:\jruby-1.7.1\/bin/jirb_swing:54:in `(root)'
4

1 回答 1

0

你有太多的目的:

 begin <--- remove this
     all_options = wait.until { driver.find_elements(:tag_name => "option") }
   end <--- remove this
   all_options.each do |option|
         if (option.attribute("value")=="11")
             option.click
         end
   end

或者,如果您需要救援:

 begin
     all_options = wait.until { driver.find_elements(:tag_name => "option") }
  rescue
     do someting
  end 
   all_options.each do |option|
         if (option.attribute("value")=="11")
             option.click
         end
   end
于 2012-12-31T08:09:38.720 回答