选择了下拉,但是watir说残疾人

时间:2013-01-29 22:44:00

标签: watir

我可以在下拉列表中设置一个选项,但后来收到错误消息。我发现一个在线网站会返回相同的错误:

http://www.100candles.com/floating_candles.htm

irb(main):124:0>  $ie = Watir::IE.attach(:url, /100candles/)
=> #<Watir::IE:0x..fd64f0146 url="http://www.100candles.com/t/Vessels?PN=1&SB=Updated" title="Vessels - 100 Candles">
irb(main):126:0> $ie.select_list(:name, "SB").flash
=> #<Watir::SelectList:0x4960ea5e located=true specifiers={:tag_name=>["select"], :name=>"SB"}>
irb(main):127:0> $ie.select_list(:name, "SB").select("Price")
Watir::Exception::ObjectDisabledException: object {:index=>0, :ole_object=>#<WIN32OLE:0x3759e58>} is disabled
    from C:/Ruby193/lib/ruby/gems/1.9.1/gems/watir-classic-3.4.0/lib/watir-classic/element.rb:329:in `assert_enabled'
    from C:/Ruby193/lib/ruby/gems/1.9.1/gems/watir-classic-3.4.0/lib/watir-classic/element.rb:470:in `perform_action'
    from C:/Ruby193/lib/ruby/gems/1.9.1/gems/watir-classic-3.4.0/lib/watir-classic/input_elements.rb:117:in `select'
    from C:/Ruby193/lib/ruby/gems/1.9.1/gems/watir-classic-3.4.0/lib/watir-classic/input_elements.rb:46:in `each'
    from C:/Ruby193/lib/ruby/gems/1.9.1/gems/watir-classic-3.4.0/lib/watir-classic/input_elements.rb:46:in `block in select'
    from C:/Ruby193/lib/ruby/gems/1.9.1/gems/watir-classic-3.4.0/lib/watir-classic/element.rb:472:in `perform_action'
    from C:/Ruby193/lib/ruby/gems/1.9.1/gems/watir-classic-3.4.0/lib/watir-classic/input_elements.rb:41:in `select'
    from (irb):127
    from C:/Ruby193/bin/irb:12:in `<main>'
irb(main):128:0>

1 个答案:

答案 0 :(得分:4)

<强>问题

选择适用于某些选项(前批量成本)但不适用于其他选项(前价格)。查看SelectList的watir代码#select:

def select(item)
  matching_options = []
  perform_action do
    matching_options = matching_items_in_select_list(:text, item) +
      matching_items_in_select_list(:label, item) +
      matching_items_in_select_list(:value, item)
    raise NoValueFoundException, "No option with :text, :label or :value of #{item.inspect} in this select element" if matching_options.empty?
    matching_options.each(&:select)
  end
  matching_options.first.text
end

页面html:

<select id="SB" onchange="document.location='/floating_candles.htm?PN=1&amp;SB=' + this.value;" name="SB">
    <optgroup style="font-style: normal;" label="Please Choose">
    <option value="">-
    <option value="PieceCost">Bulk Cost
    <option value="Updated">New Arrivals
    <option value="Popularity">Popularity
    <option value="Price">Price
    <option selected="" value="Relevance">Relevance
    </option></optgroup>
</select>

可以看到异常的原因:

  1. 在SelectList#select中,它使用与指定条件匹配的文本,标签或值收集所有选项。对于“价格”(以及其他失败的选项),收集选项(即添加到matching_options)两次 - 一次用于按文本匹配,一次用于按值匹配。相比之下,有效的(批量成本)仅匹配一次,因为它们的文本和值不同。
  2. SelectList #select将选择每个matching_option。在“价格”的情况下,这意味着两个选择。
  3. 页面上的选择列表包含一个刷新页面的onchange事件。在“价格”的情况下,选项的第一个选项有效(在观看脚本时可以看到)。但是,它是第二个失败的选择。我假设在页面刷新时选择尝试,导致异常。
  4. <强>解决方案

    解决方案是直接选择option元素。这样可以确保只选择一次选项:

    $ie.select_list(:name, "SB").option(:text => "Price").select