2

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.

3
  • I think you are sending the shift key to an 'option', is that right? What if you send the 'shift' key to the 'select' element after selecting the first option and then select the last option? Commented Oct 29, 2011 at 23:30
  • @nilesh: I have updated the code in the question, tried sending :shift to select element, still does not work Commented Oct 31, 2011 at 10:23
  • I find myself wondering if we need a wishlist item for sending keyboard modified clicks.. ala shift_click, control_click, option_click, etc. it would seem that this is something users can do for multi-select lists etc, so we should have an easy way to simulate that which does not require convoluted combinations of sendkeys and clicks Commented Oct 31, 2011 at 21:44

3 Answers 3

1

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 }
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, I know how to select all options one by one. I was playing with shift and I was surprised to see that it does not work.
0

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.

1 Comment

Looks like you can only send :shift and :left_shift: watirwebdriver.com/sending-special-keys
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.