我可以在下拉列表中设置一个选项,但后来收到错误消息。我发现一个在线网站会返回相同的错误:
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>
答案 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&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>
可以看到异常的原因:
<强>解决方案强>
解决方案是直接选择option元素。这样可以确保只选择一次选项:
$ie.select_list(:name, "SB").option(:text => "Price").select