3

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

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>
4

1 回答 1

5

问题

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

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 将选择每个匹配选项。在“价格”的情况下,这意味着两个选择。
  3. 页面上的选择列表有一个刷新页面的 onchange 事件。在“价格”的情况下,选项的第一个选择有效(在观看脚本时可以看到)。但是,失败的是第二个选择。我假设在页面刷新时选择尝试,导致异常。

解决方案

解决方法是直接选择option元素。这可确保该选项仅被选中一次:

$ie.select_list(:name, "SB").option(:text => "Price").select
于 2013-01-30T03:40:02.803 回答