Watir select_list options issue?

287 views
Skip to first unread message

krizzo

unread,
Apr 24, 2012, 3:30:04 PM4/24/12
to watir-...@googlegroups.com
I created this test script because I was having issues with another script that needed to work with select_list. This is having the same issue. From my understanding "contents[0]" should print out "Firefox" but instead I get a memory location. I printed the "memory" location out and it's not a simple array of the options it almost looks like the browser object.

I wanted to know if this was a known issue or is my understanding incorrect on options returning an array?

## Output 
#<Watir::Option:0x816f138>
[
    [0] "test",
    [1] "one",
    [2] "two",
    [3] "three"
]

## Test Script
require 'rubygems'
require 'ap'
require 'watir-webdriver'
myArray = %w[test one two three] 
b = Watir::Browser.new :firefox, :profile => 'LoL'
b.goto url
contents = Array.new
contents = b.select_list(:name => 'entry.6.single').options
puts contents[0]
ap myArray
b.close

Dan

unread,
Apr 24, 2012, 4:05:26 PM4/24/12
to watir-...@googlegroups.com
I'm not sure what you're trying to do exactly, but you need to tell it you're looking for the text 

puts contents[0].text

You also may want to consider something like this instead of an array, but again I'm not really sure what you're trying to do.

b.select_list(:name => 'entry.6.single').options.each do |opt|
  p opt.text
end

Chuck van der Linden

unread,
Apr 24, 2012, 4:29:57 PM4/24/12
to watir-...@googlegroups.com
select_list.options returns an object of type OptionCollection  not an array

It supports most of the methods you'd expect for an array in terms of simple navigation (.each, .first, [], .length, etc) so you can pretty much treat it as an array, but you can't (I'm pretty sure) assign it to an array without first converting it into an array.  

See here for more info on that object type.  http://rubydoc.info/github/watir/watir-webdriver/Watir/OptionCollection

Most of the methods such as [] or .each would return an option object, the methods for which you can see here  http://rubydoc.info/github/watir/watir-webdriver/Watir/Option

I'm pretty sure you'd want to call the .to_a method on the objectcollection to get an array. I'm not sure exactly what will be in the resulting array when it turns the collection into an array, it might be a set of hashes or something given that each option object has at least two properties if not three (value, text, selected?)

Or just trying assigning it to a variable, and see if that works

to see the details on each option in the list, try something along these lines

    b.select_list(:name => 'entry.6.single').options.each do |option|
      puts "the option is listed as: #{option.text} and selected? returns: #{option.selected?}"
    end


krizzo

unread,
Apr 24, 2012, 4:34:47 PM4/24/12
to watir-...@googlegroups.com
I wanted to get the list of options and then have it select one of them. Interesting that I have to include the .text to dereference the string. That worked thanks for the help. I was searching all over trying to find how to get the value so I could select it.

krizzo

krizzo

unread,
Apr 24, 2012, 4:39:09 PM4/24/12
to watir-...@googlegroups.com
Interesting I was thrown off when it looked like some object instead of a simple array. In the documentation it stated " NOTE: contents will be an array " which made me confused thanks for the clarification on it.

krizzo

Chuck van der Linden

unread,
Apr 24, 2012, 4:43:18 PM4/24/12
to watir-...@googlegroups.com
If you want to select a specific option, there's a much easier way.

Just use the select_list's .select method

    b.select_list(:name => 'entry.6.single').select 'Chrome'

Chuck van der Linden

unread,
Apr 24, 2012, 4:45:27 PM4/24/12
to watir-...@googlegroups.com
Note that if it is a multi-select list you may want to clear all the selections first with the .clear method, before selecting the one(s) you want to be selected.

krizzo

unread,
Apr 24, 2012, 4:57:23 PM4/24/12
to watir-...@googlegroups.com
Yeah that's what I'm doing only I wanted to program my script as if I didn't know what the value in the selection would be and wanted to select either the nth element in the list as this code does or check the values and pick one.

contents = b.select_list(:xpath, "LIST").options
value = contents[1].text
b.select_list(:xpath, "LIST").select value

Chuck van der Linden

unread,
Apr 24, 2012, 5:21:46 PM4/24/12
to watir-...@googlegroups.com
That would still be doing it the hard way..

    b.select_list(:id => "entry_6").options[1].select

If you are not sure that there will always be a 1th element in the select list, you might want to check the .length first

  assert b.select_list(:id => "entry_6").options.length >= 3
  b.select_list(:id => "entry_6").options[2].select
Reply all
Reply to author
Forward
0 new messages