无法读取span元素

时间:2013-01-02 12:53:49

标签: ruby webdriver watir

嗨,因为我做了gem install watir watir-classic我的脚本不再读取span元素了。你能帮我解决这个问题吗

这是我的例子

来源

Rented 
<SPAN class="displayData" style="padding-left:2px; width: 15;"><span name="tab4RateInfoForm.vehicleGroup"/>F</span></SPAN>&nbsp;

我的代码

carGroup=browser.span(:name => 'tab4RateInfoForm.vehicleGroup').text

错误消息

C:/Ruby193/lib/ruby/gems/1.9.1/gems/watir-classic-3.3.0/lib/watir-classic/locator.rb:79:in `rescue in match?': name is an unknown way of finding a <span> element (tab4RateInfoForm.vehicleGroup) (Watir::Exception::MissingWayOfFindingObjectException)
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/watir-classic-3.3.0/lib/watir-classic/locator.rb:76:in `match?'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/watir-classic-3.3.0/lib/watir-classic/locator.rb:62:in `block in match_with_specifiers?'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/watir-classic-3.3.0/lib/watir-classic/locator.rb:59:in `each'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/watir-classic-3.3.0/lib/watir-classic/locator.rb:59:in `all?'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/watir-classic-3.3.0/lib/watir-classic/locator.rb:59:in `match_with_specifiers?'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/watir-classic-3.3.0/lib/watir-classic/locator.rb:22:in `block (2 levels) in each'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/watir-classic-3.3.0/lib/watir-classic/locator.rb:140:in `block in each_element'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/watir-classic-3.3.0/lib/watir-classic/locator.rb:139:in `each'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/watir-classic-3.3.0/lib/watir-classic/locator.rb:139:in `each_element'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/watir-classic-3.3.0/lib/watir-classic/locator.rb:21:in `block in each'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/watir-classic-3.3.0/lib/watir-classic/locator.rb:20:in `each'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/watir-classic-3.3.0/lib/watir-classic/locator.rb:20:in `each'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/watir-classic-3.3.0/lib/watir-classic/locator.rb:150:in `locate'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/watir-classic-3.3.0/lib/watir-classic/element.rb:33:in `locate'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/watir-classic-3.3.0/lib/watir-classic/element.rb:63:in `assert_exists'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/watir-classic-3.3.0/lib/watir-classic/element.rb:132:in `text'

2 个答案:

答案 0 :(得分:4)

解决方案1:使用css定位器

您必须使用css-locator(或xpath):

carGroup = browser.span(:css, 'span[name="tab4RateInfoForm.vehicleGroup"]').text

解决方案2:猴子补丁

或者,如果你有许多使用name属性的遗留脚本,你可以修补Watir以获得所有元素的名称方法(或者如果需要,可以使用特定元素)。

将以下内容添加到您需要的地方:

Watir::IE.new(true)
module Watir
  class Element
    def name
      return self.attribute_value('name')
    end
  end
end

您的原始方法现在应该有效。

答案 1 :(得分:0)

看起来无法再通过span属性访问name元素。

尝试此操作(使用通用element代替span):

carGroup=browser.element(:name => 'tab4RateInfoForm.vehicleGroup').text