Željko Filipin
Željko Filipin

Reputation: 57282

Selecting multiple options in select list with shift

I have HMTL like this:

<select multiple="multiple">
  <option value="volvo">Volvo</option>
  <option value="saab">Saab</option>
  <option value="mercedes">Mercedes</option>
  <option value="audi">Audi</option>
</select>

I would like to select all options by clicking the first one, holding shift and clicking the last one:

browser.select.select "Volvo"
browser.select.send_keys :shift
browser.select.select "Audi"

But just the first and the last one are selected.

browser.select.selected_options
 => ["Volvo", "Audi"]

Am I doing something wrong?

Environment: Mac OS X 10.6.8, Firefox 7.0.1, ruby 1.9.2p290, selenium-webdriver 2.10.0, watir-webdriver 0.3.5.

Upvotes: 2

Views: 2047

Answers (3)

Chuck van der Linden
Chuck van der Linden

Reputation: 6660

With regard to Watir-Webdriver I asked Jari (the main driving force behind Watir-webdriver) about this and got the following answer, which may give you a path to pursue if you need this badly enough

There's nothing exposed in watir-webdriver (yet), but you should be able to do it with the actions API in WebDriver (see http://rubydoc.info/gems/selenium-webdriver/2.10.0/Selenium/WebDriver/ActionBuilder). I'm not sure how well-supported this is across browsers - but from the Java tests (which are the most extensive) it looks like it's currently only supported on Firefox + Linux:

http://code.google.com/p/selenium/source/browse/trunk/java/client/test/org/openqa/selenium/interactions/CombinedInputActionsTest.java#50

I also created a feature-request in the Watir-Webdriver project on Github, if this is something you need, you may want to comment on it to make your needs known.

Upvotes: 0

anonygoose
anonygoose

Reputation: 739

I'm not familiar with sendkeys enough to be sure about this, but I'd imagine what it is doing is saying: shift keyDown followed immediately by shift keyUp , so that you aren't getting time to click before the keyUp event. Unless of course the :shift token is doing something special. Like I said I'm not at all familiar with it.

The send key list (not specifically web driver) lists

Send("{a down}") ;Holds the A key down

Send("{a up}") ;Releases the A key

as ways of singularly firing off those events (i.e. simulating the key being held down).

Perhaps watir-webdriver has a similar functionality?

browser.element.sendkeys [:shift down]

Worth a shot.

Upvotes: 0

Alister Scott
Alister Scott

Reputation: 3685

Not sure why shift doesn't work, but there's a very easy way to achieve what you want:

browser.select_list.options.each { |option| option.select }

You may want to clear the list first in case any are already selected:

browser.select_list.clear
browser.select_list.options.each { |option| option.select }

Upvotes: 1

Related Questions