Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

dropdown selected but watir says disabled

Tags:

watir

I'm able to set an option in a dropdown list, but then I get an error message. 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>
like image 738
user1516021 Avatar asked Dec 06 '25 08:12

user1516021


1 Answers

Problem

The selection works for some options (ex Bulk Cost) but not others (ex Price). Looking 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 page 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>

The reason for the exception can be seen:

  1. In SelectList#select, it gathers up all options with the text, label or value that matches the specified criteria. For "Price" (and the other options that fail), the option is collected (ie added to matching_options) twice - once for matching by text and once for matching by value. In contrast, the ones that work (ex Bulk Cost) only get matched once since their text and value is different.
  2. SelectList#select will select each matching_option. In the case of "Price", this means two selections.
  3. The select list on the page has an onchange event that refreshes the page. In the case of "Price", the first selection of the option works (as can be seen when watching the script). However, it is the second selection that fails. I assume that the selection attempts while the page is refreshing, resulting in the exception.

Solution

The solution is to directly select the option element. This ensures that the option only gets selected once:

$ie.select_list(:name, "SB").option(:text => "Price").select
like image 182
Justin Ko Avatar answered Dec 10 '25 13:12

Justin Ko



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!