Dropdown is selected but watir says it is disabled
I can set an option in the dropdown, but then I get an error. I found an online site that will return the same error:
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>
+3
source to share
1 answer
Problem
The selection works for some options (ex Bulk Cost) but not others (ex Price). Look at the watir code for SelectList # 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
And the html page:
<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>
The reason for the exception can be seen:
- In SelectList # select, it collects all options with text, label, or value that match the specified criteria. For "Price" (and other parameters that are not met), the option is collected (ie added to match_options) twice - once for text matching and once for value matching. In contrast, those that work (ex Bulk Cost) only get it once since their text and meaning are different.
- SelectList # select will select each match_option. In the case of "Price", this means two choices.
- The select list on the page has an onchange event that refreshes the page. In the case of "Price", the first choice of the option works (as seen when viewing the script). However, this is the second choice that fails. I am guessing the selection is trying while the page is being refreshed, resulting in an exception.
Decision
The solution is to directly select the option element. This ensures that the option is only selected once:
$ie.select_list(:name, "SB").option(:text => "Price").select
+4
source to share