3

嗨,因为我做了 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'
4

2 回答 2

4

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

您将不得不使用 css-locator(或 xpath):

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

解决方案 2:猴子补丁

或者,如果您有很多使用 name 属性的遗留脚本,您可以修改 Watir 以使所有元素(或特定元素,如果需要)具有 name 方法。

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

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

您原来的方法现在应该可以工作了。

于 2013-01-02T15:36:21.003 回答
0

看起来span元素不能再通过name属性访问。

试试这个(使用通用element而不是span):

carGroup=browser.element(:name => 'tab4RateInfoForm.vehicleGroup').text
于 2013-01-02T13:29:35.330 回答